: This article mainly introduces the php file Upload FAQ (basic). If you are interested in the PHP Tutorial, refer to it.
Since the previous article "php file name garbled problem for uploading Chinese Files" encountered a file upload problem, simply summarize several problems that php often encountered when uploading files, you will not need to find it later.
1. First make the simplest Upload file
1 2 3
4 5 6 7
9
Filename:10
11
12
13
14 15 16
1
0) 5 { 6 echo "Return Code: " . $_FILES["file"]["error"] . "
"; 7 } 8 else 9 {10 echo "Upload: " . $_FILES["file"]["name"] . "
";11 echo "Type: " . $_FILES["file"]["type"] . "
";12 echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";13 echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";14 15 if (file_exists("upload/" . $_FILES["file"]["name"]))16 {17 echo $_FILES["file"]["name"] . " already exists. ";18 }19 else20 {21 move_uploaded_file($_FILES["file"]["tmp_name"],22 "upload/" . $_FILES["file"]["name"]);23 echo "Stored in: " . "upload/" . $_FILES["file"]["name"];24 }25 }26 }27 else28 {29 echo "Invalid file";30 }31 ?>
2. then understand the value of the Super global variable $ _ FILES.
$ _ FILES ['userfile'] ['name']
$ _ FILES ['userfile'] ['type']
$ _ FILES ['userfile'] ['size']
$ _ FILES ['userfile'] ['tmp _ name']
$ _ FILES ['userfile'] ['error']
All values of $ _ FILES ['userfile'] ['error:
The value of UPLOAD_ERR_ OK is 0. If no error occurs, the file is uploaded successfully.
The value of UPLOAD_ERR_INI_SIZE is 1. the uploaded file exceeds the limit of the upload_max_filesize option in php. ini.
The value of UPLOAD_ERR_FORM_SIZE is 2. the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.
The value of UPLOAD_ERR_PARTIAL is 3, and only part of the file is uploaded.
The value of UPLOAD_ERR_NO_FILE is 4, and no file is uploaded.
The value of UPLOAD_ERR_NO_TMP_DIR is 6, and the temporary folder cannot be found. PHP 4.3.10 and PHP 5.0.3 are introduced.
The value of UPLOAD_ERR_CANT_WRITE is 7, and file writing fails. PHP 5.1.0 is introduced.
3. many situations: you must strictly determine the type of the uploaded file.
We know that it is unwise to use $ _ FILES ['userfile'] ['type'] to determine the type of the uploaded file, because the judgment is based on the file suffix name, anyone can change the suffix of an mp3 file to jpg to pretend to be an image for upload. Therefore, php officially recommends using php extension php_fileinfo to determine the mime of the file, there are many ways to start expansion: Windows and linux are slightly different.
4. Scenario 1: Automatically rename the uploaded file after it is renamed.
1 if (file_exists (". /upload /". $ _ FILES ["file"] ["name"]) 2 {3 do {4 $ suffix = ""; 5 $ suffix_length = 4; 6 $ str = "0123456789 abcdefghijklmnopqrstuvwxyz"; 7 $ len = strlen ($ str)-1; 8 // append four random characters 9 for ($ I = 0; $ I <$ suffix_length; $ I ++) {10 $ suffix. = $ str [rand (0, $ len)]; 11} 12 $ upload_filename = $ _ FILES ['file'] ['name']; 13 $ filename = substr ($ upload_filename, 0, strrpos ($ upload_filename ,". ")). $ suffix. ". ". substr ($ upload_filename, strrpos ($ _ FILES ["file"] ["name"], ". ") + 1); 14} while (file_exists (". /upload /". $ filename); 15 move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], ". /upload /". $ filename); 16} else {17 move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "upload /". $ _ FILES ["file"] ["name"]); 18}
5. Scenario 2: upload files to directories by date
1 $structure = './'.date("Y").'/'.date("m").'/'.date("d").'/';2 3 4 if (!mkdir($structure, 0777, true)) {5 die('Failed to create folders...');6 }7 8 move_uploaded_file($_FILES["file"]["tmp_name"],$structure . $_FILES["file"]["name"]);
6. Scenario 3: multi-file Upload
1
2
Pictures:3 4 5 6 7
8
1
$error) {3 if ($error == UPLOAD_ERR_OK) {4 $tmp_name = $_FILES["pictures"]["tmp_name"][$key];5 $name = $_FILES["pictures"]["name"][$key];6 move_uploaded_file($tmp_name, "data/$name");7 }8 }9 ?>
In some cases, the variable structure of multiple files is not easy to use:
Array (1 ){
["Upload"] => array (2 ){
["Name"] => array (2 ){
[0] => string (9) "file0.txt"
[1] => string (9) "file1.txt"
}
["Type"] => array (2 ){
[0] => string (10) "text/plain"
[1] => string (10) "text/html"
}
}
}
In many cases, we need a structure similar to this.
Array (1 ){
["Upload"] => array (2 ){
[0] => array (2 ){
["Name"] => string (9) "file0.txt"
["Type"] => string (10) "text/plain"
},
[1] => array (2 ){
["Name"] => string (9) "file1.txt"
["Type"] => string (10) "text/html"
}
}
}
Use the following function to easily convert the structure
1 function perse_array($vector) { 2 $result = array(); 3 foreach($vector as $key1 => $value1) 4 foreach($value1 as $key2 => $value2) 5 $result[$key2][$key1] = $value2; 6 return $result; 7 } 8 $upload = perse_array($_FILES["upload"]);
7. sometimes, you need to configure the server to modify the maximum size of uploaded files.
First, on the form
You can restrict the size of the uploaded file (which can be bypassed ).
Then you need to adjust the configuration on the server.
Php. ini:
Max_execution_time = 30 maximum time for each script to run, in seconds
Max_input_time = 60. the time that each script can consume, in seconds.
Memory_limit = 128 M, which is the maximum memory consumed by script operation.
Post_max_size = 8 M, and the maximum data size for form submission is 8 M. This option does not limit the size of a single file to be uploaded, but restricts the data submitted for the entire form.
Upload_max_filesize = 2 M, maximum file upload license size
Nginx:
1 location / {2 root html;3 index index.html index.htm;4 client_max_body_size 1000m;5 }
The above describes the php file Upload FAQ (basic), including the content, hope to be helpful to friends who are interested in PHP tutorials.