Php file upload and error code summary

Source: Internet
Author: User
Tags html form php file upload

Upload operation code

The code is as follows: Copy code

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml" xml: lang = "en">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> </title>
</Head>
<Body>
 
<? Php
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'].'! <Br> '; // output if the file is uploaded successfully
?>
</Body>
</Html>

Html

The code is as follows: Copy code

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Html xmlns = "http://www.w3.org/1999/xhtml" xml: lang = "en">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> </title>
</Head>
<Body>
<Form action = "upload. php" method = "post" enctype = "multipart/form-data">
<Input type = "hidden" name = "MAX_FILE_SIZE" value = "1000000">
Select a file: <input type = "file" name = "myfile">
<Input type = "submit" value = "Upload file">
</Form>
</Body>
</Html>

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.