Php file Upload FAQ summary ,. Summary of php file uploading common problems. Let's summarize several common problems encountered during php file uploading. you will not need to find them later. 1. First, make a summary of the most simple Upload file copying FAQs for php file uploading,
Summarize several problems that php often encounters when uploading files. you don't need to find them again later.
1. First make the simplest Upload file
The code is as follows:
The code is as follows:
<? Php
If ($ _ FILES ["file"] ["size"] <20000)
{
If ($ _ FILES ["file"] ["error"]> 0)
{
Echo "Return Code:". $ _ FILES ["file"] ["error"]."
";
}
Else
{
Echo "Upload:". $ _ FILES ["file"] ["name"]."
";
Echo "Type:". $ _ FILES ["file"] ["type"]."
";
Echo "Size:". ($ _ FILES ["file"] ["size"]/1024). "Kb
";
Echo "Temp file:". $ _ FILES ["file"] ["tmp_name"]."
";
If (file_exists ("upload/". $ _ FILES ["file"] ["name"])
{
Echo $ _ FILES ["file"] ["name"]. "already exists .";
}
Else
{
Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"],
"Upload/". $ _ FILES ["file"] ["name"]);
Echo "Stored in:". "upload/". $ _ FILES ["file"] ["name"];
}
}
}
Else
{
Echo "Invalid file ";
}
?>
2. then understand the value of the Super global variable $ _ FILES.
The code is as follows:
$ _ 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 has been renamed.
The code is as follows:
If (file_exists ("./upload/". $ _ FILES ["file"] ["name"])
{
Do {
$ Suffix = "";
$ Suffix_length = 4;
$ Str = "0123456789 abcdefghijklmnopqrstuvwxyz ";
$ Len = strlen ($ str)-1;
// Append four random characters after the file name
For ($ I = 0; $ I <$ suffix_length; $ I ++ ){
$ Suffix. = $ str [rand (0, $ len)];
}
$ Upload_filename = $ _ FILES ['file'] ['name'];
$ Filename = substr ($ upload_filename, 0, strrpos ($ upload_filename ,". ")). $ suffix. ". ". substr ($ upload_filename, strrpos ($ _ FILES ["file"] ["name"], ". ") + 1 );
} While (file_exists ("./upload/". $ filename ));
Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "./upload/". $ filename );
} Else {
Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "upload/". $ _ FILES ["file"] ["name"]);
}
5. Scenario 2:Upload files to directories by date
The code is as follows:
$ Structure = '. /'. date ("Y "). '/'. date ("m "). '/'. date ("d "). '/';
If (! Mkdir ($ structure, 0777, true )){
Die ('failed' to create folders ...');
}
Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], $ structure. $ _ FILES ["file"] ["name"]);
6. Scenario 3:Multi-file Upload
The code is as follows:
The code is as follows:
<? Php
Foreach ($ _ FILES ["pictures"] ["error"] as $ key => $ error ){
If ($ error = UPLOAD_ERR_ OK ){
$ Tmp_name = $ _ FILES ["pictures"] ["tmp_name"] [$ key];
$ Name = $ _ FILES ["pictures"] ["name"] [$ key];
Move_uploaded_file ($ tmp_name, "data/$ name ");
}
}
?>
In some cases, the variable structure of multiple files is not easy to use:
The code is as follows:
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.
The code is as follows:
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
The code is as follows:
Function perse_array ($ vector ){
$ Result = array ();
Foreach ($ vector as $ key1 => $ value1)
Foreach ($ value1 as $ key2 => $ value2)
$ Result [$ key2] [$ key1] = $ value2;
Return $ result;
}
$ 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:
The code is as follows:
Location /{
Root html;
Index index.html index.htm;
Client_max_body_size 1000 m;
}
These are the solutions to common problems. I hope you will like them.
Summary: Let's summarize several problems that php often encounters when uploading files. you will not need to find them again later. 1. make the simplest copy of uploaded files...