This article provides a summary of some php File Upload problems. For more information, see
This article provides a summary of some php File Upload problems. For more information, see
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 is 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: