Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.

upload_file

Uploading files can be difficult, and even more difficult is making sure it's secure so no unwanted files gets uploaded to your server. The upload_file() function puts you in charge and enables you with many settings to specify what kind of files which are allowed uploaded, file size and where the uploaded file should be stored.

upload_file

Quote

upload_file ( string $source_file [ , string $target_file [ , string $target_folder [ , string $valid_ext [ , numeric $max_size [ , string $query ] ] ] ] ] )

Uploading files can be difficult, and even more difficult is making sure it's secure so no unwanted files gets uploaded to your server. The upload_file() function puts you in charge and enables you with many settings to specify what kind of files which are allowed uploaded, file size and where the uploaded file should be stored.

Parameters
source_file
The key of the $_FILE which holds the uploaded file

target_file
Name for the uploaded file, leave this blank to use the uploaded file's name.

target_folder
Folder the uploaded file will be moved to. Default is /downloads/

valid_ext
Valid file extensions for uploaded files. Defaults are .zip, .rar, .tar, .bz2 and .z7.

max_size
Maximum allowed file size. Default is 15000 bytes.

query
Query when the file is uploaded.

Return Values
Returns an array with information about the upload:

Quote

Array
(
[source_file] => Name of $_FILE variable for the uploaded file,
[source_size] => Size of the uploaded file in bytes,
[source_ext] => Extension for the uploaded file,
[target_file] => Name of the uploaded file with file extension,
[target_folder] => Name of the folder the file has been uploaded to,
[valid_ext] => All valid extensions for the upload,
[max_size] => Max file size allowed for the uploaded file.
[query] => Query when file was uploaded.
[error] => Error number for file upload.
0 - No error.
1 - Maximum file size exceeded.
2 - Invalid file extension.
3 - Invalid query string.
4 - File not uploaded.
)


Examples

Basic upload_file() example
Code
<?php
require_once INCLUDES."infusions_include.php";
 
// Name of $_FILE key which holds the upload
$source_file = "myfile";
 
// Left blank to use the filename as it is
$target_file = "";
 
// Upload folder
$target_folder = DOWNLOADS;
 
// Valid file extensions
$valid_ext = ".zip|.rar|.tar|.bz2|.z7";
 
// Maximum file size in bytes
$max_size = "15000";
 
$upload = upload_file($source_file, $target_file, $target_folder, $valid_ext, $max_size);
 
if ($upload['error'] == 1) {
 // Maximum file size exceeded
} elseif ($upload['error'] == 2) {
 // Invalid file extension
} elseif ($upload['error'] == 3) {
 // Invalid query string
} elseif ($upload['error'] == 4) {
 // File not uploaded
} else {
 // Successful upload!
}
?>


Advanced upload_file() example
Code
<?php
if (isset($_POST['uploadfile']) {
 $upload = upload_file("myfile", "", DOWNLOADS, ".zip|.rar|.tar|.bz2|.z7", "15000");
 if ($upload['error'] == 0) {
 opentable("Success");
 echo "The file was uploaded successfully.";
 closetable();
 } else {
 opentable("Error ".$upload['error']);
 if ($upload['error'] == 1) {
 echo "The uploaded file exceeded the upload limit ofn";
 echo "<strong>".parsebytesize($upload['max_size'])."</strong>.";
 } elseif ($upload['error'] == 2) {
 echo "The uploaded file has an invalid file extension,n";
 ecgo "valid extensions are <strong>".$upload['valid_ext']."</strong>.";
 } elseif ($upload['error'] == 3) {
 echo "An invalid query, please check your query: ".$upload['query'].".";
 } elseif ($upload['error'] == 4) {
 echo "The file was for some reason not uploaded.";
 }
 closetable();
 }
}
 
opentable("Upload File");
echo "<form action='".FUSION_SELF."' method='post' enctype='multipart/form-data'>n";
echo "Filename: <input type='file' name='myfile' />n";
echo "<input type='submit' name='uploadfile' value='Upload' />n";
echo "</form>n";
closetable();
?>


Changelog
7.01.00 - Function added to PHPFusion