When files are submitted to an upload form, they are uploaded into a temporary directory and a multidimensional array containing information about them is returned. These files are temporary and must be copied to another location.
The form below would return an array something like the array below it.
The uploaded file exceeds the upload_max_filesize directive in php.ini.
2
The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
3
The uploaded file was only partially uploaded.
4
No file was uploaded.
5
(not used)
6
Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
7
Failed to write file to disk. Introduced in PHP 5.1.0.
8
File upload stopped by extension. Introduced in PHP 5.2.0.
You can if ( $_FILES[file_name][error] > 0 ) to see if there was an error followed by a
switch statement to tell the user what the error was. If the files passes the error check, you can use the values in the $_FILES array to filter user input. For example, if you only want image files entered into the form above, you can make an array of image MIME types and reject any file that doesn't have a MIME type in that array.
<? if ( $_FILES[file_upload][type] ) { $type = strtolower($_FILES[file_upload][type]); echo "MIME = <br>"; $allowed = array( "image/jpeg", "image/jpg", "image/gif", "image/png", "image/x-png", "image/pjpeg" ); if ( !in_array( $type, $allowed) ) { die("<center><h1>File type is not allowed</h1></center>"); } } ?>
<?
######################################
# change password to your own.
# set path to your uploads directory.
# upload directory must have 777 permissions.
######################################
$password = "your_password";
$path = "uploads/";
######################################
session_start();
#### log out
$self = "$_SERVER[PHP_SELF]";
if ( isset($_POST[logout]) )
{
$_SESSION[pass] = "";
header("location:$self");
exit;
}
####
<?
######## start file check ########
if ( $_FILES )
{
######## check for errors ########
if ( $_FILES[file_upload][error] > 0 )
{
echo "<div class=\"error\">
The file could not be upoaded because ";
switch ( $_FILES[file_upload][error] )
{
case 1:
echo "the file is too big.";
break;
case 2:
echo "the file is too big.";
break;
case 3:
echo "the file was only partially uploaded.";
break;
case 4:
echo "no file was uploaded.";
break;
case 6:
echo "no temporary folder was available.";
break;
case 7:
echo "unable to write to disk.";
break;
case 8:
echo "file upload stopped";
break;
default:
echo "a system error occured.";
} # end of switch
echo "</div></body></html>";
exit;
} # end if files > 0
################
#### check file type ####
$type = strtolower($_FILES[file_upload][type]);
$allowed = array( "image/jpeg", "image/jpg", "image/gif", "image/png", "image/x-png", "image/pjpeg" );
if ( !in_array( $type, $allowed) )
{
echo "<div class=\"error\">File type $type is not allowed</h1></div>
</body></html>";
exit;
} # end allowed check
####
#### check original file name ####
$old_name = trim($_FILES[file_upload][name]);
$old_name = preg_replace("@[^\w\.]@", "_", $old_name );
####
#### check new file name ####
if ( !empty($_POST[file_name]) )
{
$file_name = trim("$_POST[file_name]");
$file_name = preg_replace("@[^\w\.]@", "_", $file_name);
}
# if no new name then name = original name
else { $file_name = $old_name; }
####