Php file upload and error code summary. This article summarizes the introduction of PHP file upload and error code. For more information, see this article. The upload operation code is as follows: Copy the code! DOCTYPEhtmlPUBL This article summarizes the introduction of PHP file upload and error code. if you need to learn, refer to this article.
Upload operation code
| The code is as follows: |
|
|
If ($ _ FILES ['myfile'] ['error']> 0) {// checks whether the file is successfully uploaded to the server. 0 indicates that the file is successfully uploaded.
Echo 'Error :';
Switch ($ _ FILES ['myfile'] ['error']) {
Case UPLOAD_ERR_ OK:
$ Response = 'There is no error, the file uploaded with success .';
Break;
Case UPLOAD_ERR_INI_SIZE:
$ Response = 'the uploaded file exceeds The upload_max_filesize directive in php. ini .';
Break;
Case UPLOAD_ERR_FORM_SIZE:
$ Response = 'the uploaded file exceeds The MAX_FILE_SIZE direve VE that was specified in the HTML form .';
Break;
Case UPLOAD_ERR_PARTIAL:
$ Response = 'the uploaded file was only partially uploaded .';
Break;
Case UPLOAD_ERR_NO_FILE:
$ Response = 'No file was uploaded .';
Break;
Case UPLOAD_ERR_NO_TMP_DIR:
$ Response = 'missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3 .';
Break;
Case UPLOAD_ERR_CANT_WRITE:
$ Response = 'failed' to write file to disk. Introduced in PHP 5.1.0 .';
Break;
Case UPLOAD_ERR_EXTENSION:
$ Response = 'File upload stopped by extension. Introduced in PHP 5.2.0 .';
Break;
Default:
$ Response = 'unknown error ';
Break;
}
Echo $ response;
Exit; // if $ _ FILES ['myfile'] ['error'] is greater than 0, an error occurs. output the error message and exit the program.
}
// Obtain the primary and subtypes in the MIME type of the uploaded file
List ($ maintype, $ subtype) = explode ("/", $ _ FILES ['myfile'] ['type']);
If ($ maintype = "text") {// The Upload cannot be uploaded because the master node is used, such as. txt. html. php.
Echo: text files cannot be uploaded. ';
Exit; // exit the program if the user uploads a text file
}
$ Upfile = './uploads/'. time (). $ _ FILES ['myfile'] ['name']; // defines the uploaded location and new file name
If (is_uploaded_file ($ _ FILES ['myfile'] ['tmp _ name']) {// Determine whether the file is uploaded
If (! Move_uploaded_file ($ _ FILES ['myfile'] ['tmp _ name'], $ upfile) {// move a file from
Echo: the file cannot be moved to the specified directory. ';
Exit;
}
} Else {
Echo 'problem: uploading a file is not a legal file :';
Echo $ _ FILES ['myfile'] ['name'];
Exit;
}
Echo 'File: '. $ upfile.' uploaded successfully. The size is '. $ _ FILES ['myfile'] ['size'].'!
'; // Output if the file is uploaded successfully
?>
|
Html
| The code is as follows: |
|
|
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
|
Some common error codes when uploading files
0 | UPLOAD_ERR_ OK | the file is successfully uploaded.
1 | UPLOAD_ERR_INI_SIZE | Size exceeds upload_max_filesize in php. ini.
2 | UPLOAD_ERR_FORM_SIZE | Size exceeds MAX_FILE_SIZE specified in HTML form.
3 | UPLOAD_ERR_PARTIAL | the file is not completely uploaded.
4 | UPLOAD_ERR_NO_FILE | no file was uploaded.
5 | UPLOAD_ERROR_E | As expliained by @ Progman, removed in rev. 81792
6 | UPLOAD_ERR_NO_TMP_DIR | the temporary folder cannot be found.
7 | UPLOAD_ERR_CANT_WRITE | the disk cannot be written.
8 | UPLOAD_ERR_EXTENSION | File upload stopped by extension.
Bytes. The upload operation code is as follows! DOCTYPE html PUBL...