-->The server (temporary file)--> specifies the directory, and when the file enters the server it is a temporary file, in which case the temporary file name Tmp_name.
Setting limits on the client to upload files (file type and size) is unsafe because the customer can modify the limit through the source code, so set the limit here on the server.
Set the encoding to UTF-8 to avoid the Chinese garbled header (' content-type:text/html;charset=utf-8 ');
Receive uploaded information through $_files $fileInfo = $_files[' MyFile ']; function UploadFile ($fileInfo, $uploadPath = ' uploads ', $flag =true, $allowExt =array (' jpeg ', ' jpg ', ' png ', ' gif '), $ MaxSize = 2097152 {//Judge error number, only 0 or UPLOAD_ERR_OK, no error occurred, upload success if ($fileInfo [' Error ']>0) {//Note!
Error message not 5 switch ($fileInfo [' ERROR ']) {Case 1: $mes = ' upload file exceeds the value of upload_max_filesize option in PHP configuration file ';
Break
Case 2: $mes = ' exceeds the size of the HTML form max_file_size limit ';
Break
Case 3: $mes = ' file part uploaded ';
Break
Case 4: $mes = ' no upload file selected ';
Break
Case 6: $mes = ' No temporary directory found ';
Break
Case 7: $mes = ' file write Failed ';
Break
Case 8: $mes = ' uploaded file interrupted by php extender ';
Break
Exit ($mes);
$ext =pathinfo ($fileInfo [' name '],pathinfo_extension);
$ALLOWEXT =array (' jpeg ', ' jpg ', ' png ', ' gif '); Detects the type of uploaded file if (in_array($ext, $ALLOWEXT))
{exit (' illegal file type ');
//Check upload text size is compliant/$maxSize = 2097152;//2M if ($fileInfo [' Size ']> $maxSize) {exit (' upload file too large ');
///Detect if the picture is a true picture type//$flag =true;
if ($flag) {if (!getimagesize ($fileInfo [' tmp_name '])) {exit (' not a real picture type ');
}//detection is uploaded via HTTP post if (!is_uploaded_file ($fileInfo [' tmp_name ']) {exit (' File not uploaded via HTTP POST) ');
}//$uploadPath = ' uploads ';
If you do not have this folder, then create an if (!file_exists ($uploadPath)) {mkdir ($uploadPath, 0777, true);
chmod ($uploadPath, 0777); //new filename Unique $uniName = MD5 (Uniqid (Microtime (True), true).
$ext; $destination = $uploadPath. '
/'. $uniName;
The @ symbol is to not allow the customer to see the error message if (! @move_uploaded_file ($fileInfo [' Tmp_name '], $destination)) {exit (' file move failed ');
}//echo ' File upload success ';
return Array (//' NewName ' => $destination,//' size ' => $fileInfo [' size '],//' type ' => $fileInfo [' type ']//);
return $destination;
}?>