This is a frequently encountered problem in the project, so encapsulate one, share it to everyone.
First, pre-configuration php.ini if the upload file exceeds the PHP configuration then $_post or $_files, and so on is an empty array, this is a pit, because then can not be used $_files["UploadFile" ["size"] to obtain the file size.
- Upload_max_filesize = 8M//upload file size
- Post_max_size = 10M//upload file size with post
- Memory_limit = 20M//php script runs up to maximum occupied memory size
Two, table solid column a.php
<form action= "b.php" method= "POST" enctype= "Multipart/form-data" >
<input type= "hidden" name= "max_file_size" value= ">"
<!--If the above line is set to indicate that the form uploads a file size limit, so $_files["Upfile" ["Size"] is 0,$_files["Upfile" ["Error"] is 2, followed by "error"--
<input type= "File" Name= "Upfile" id= "Upfile"/><br/>
<br/>
<input type= "Submit"/>
</form>
Third, PHP file b.php
<?php
Get file name extension
function Get_file_extention ($fileName) {
Return End (Explode (".", $fileName));
}
Get a unique random name
function Get_uniname () {
return MD5 (Uniqid (Microtime (True), true));
}
Upload processing
function UploadFile ($fileInfo, $path = "uploads", $allowExt =array ("png", "JPG", "JPEG", "gif", "zip"), $fileMaxSize = 104857600000, $imageTag =true) {
if (! $fileInfo) {
Exit ("File information error");//May be file exceeded server limit
};
if ($fileInfo ["Error"]==0) {
Check File size
if ($fileInfo [' Size '] > $fileMaxSize) {
Exit ("File size exceeds");
}
Check for extensions
$ext = get_file_extention ($fileInfo [' name ']);
if (!in_array ($ext, $allowExt)) {
Exit ("Illegal file name");
}
Check if it is a real picture file
if (! $imageTag) {
if (!getimagesize ($fileInfo [' tmp_name '])) {
Exit ("Not a real picture file");
}
}
Check if the file is an uploaded file
if (!is_uploaded_file ($fileInfo [' tmp_name '])) {
Exit ("Non-uploading file");
}
Check if the upload directory exists
if (!file_exists ($path))
{
mkdir ($path, 0777,true);
}
$fileName = Get_uniname (). ".". $ext;
$destinationPath = $path. " /". $fileName;
Upload
if (Move_uploaded_file ($fileInfo [' Tmp_name '], $destinationPath)) {
$result =array ("result" = "image upload Success", "code" = "0000", "data" = $destinationPath);
}else{
$result = Array ("result" = "image upload failed", "code" = "1111", "data" = $destinationPath);
};
}else{
Switch ($fileInfo [' ERROR ']) {
Case 1:
$result =array ("result" and "=" exceeds the size of the profile upload file "," Code "=" 0001 "," Data "=" ");//upload_err_ini_size
Break
Case 2:
$result =array ("result" and "=" exceeds the size of the form setup upload file "," code "=" 0002 "," Data "and" "");//upload_err_form_size
Break
Case 3:
$result =array ("Result" = "file part is uploaded", "code" = "0003", "Data" and "" ");//upload_err_partial
Break
Case 4:
$result = Array ("Result" = "no file is uploaded", "code" = "0004", "Data" = "");//upload_err_no_file
Break
Case 6:
$result = Array ("Result" = "No temp directory Found", "code" = "0005", "Data" = "");//upload_err_no_tmp_dir
Break
Case 7:
$result = Array ("Result" = "File Not writable", "code" = "0006", "Data" = "");//upload_err_cant_write;
Break
Case 8:
$result = Array ("result" and "=" because the PHP extension interrupted the file upload "," code "=" 0006 "," Data "and" "");//upload_err_extension
Break
}
}
return $result;
}
?>
PHP encapsulated File upload