PHP file upload and Error code summary _php Tutorial

Source: Internet
Author: User
Tags php file upload
This article summarizes the PHP file upload and error code introduction, there are friends who need to learn to refer to this article.

Upload Operation code

The code is as follows Copy Code





<title></title>



if ($_files[' myfile ' [' Error '] > 0) {//Determine if the file can be successfully uploaded to the server, 0 indicates successful upload
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 directive that is 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 the 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 there is an error, output the error message and exit the program
}

Gets the main type and subtype in the MIME type of the uploaded file
List ($maintype, $subtype) =explode ("/", $_files[' myfile ' [' type ']);

if ($maintype = = "text") {//The text file cannot be uploaded by the master type limit, such as. txt. html. PHP and other file files

Echo ' problem: cannot upload text file. ';
Exit Exit the program if the user uploads a text file

}

$upfile = './uploads/'. Time (). $_files[' myfile ' [' name ']; Define post-upload location and new file name

if (Is_uploaded_file ($_files[' myfile ' [' tmp_name '])) {//Determine if the file is being uploaded

if (!move_uploaded_file ($_files[' myfile ' [' tmp_name '], $upfile)) {//From moving files
Echo ' problem: The file cannot be moved to the specified directory. ';
Exit
}

}else{

Echo ' problem: Uploading a file is not a legitimate file: ';
echo $_files[' myfile ' [' name '];
Exit

}

echo ' file: '. $upfile. ' Upload succeeded, size: '. $_files[' myfile ' [' Size ']. '!
'; Output if file upload is successful
?>

Html

The code is as follows Copy Code

"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >








Some common upload files when error codes

0 | UPLOAD_ERR_OK | File Upload successfully
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 | File not fully uploaded
4 | Upload_err_no_file | No files Uploaded
5 | Upload_error_e | As expliained by @Progman, removed in Rev. 81792
6 | Upload_err_no_tmp_dir | Temporary folder not found
7 | Upload_err_cant_write | Disk is not writable
8 | upload_err_extension | File upload stopped by extension.

http://www.bkjia.com/PHPjc/631586.html www.bkjia.com true http://www.bkjia.com/PHPjc/631586.html techarticle This article summarizes the PHP file upload and error code introduction, there are friends who need to learn to refer to this article. Upload operation code code as follows copy code! DOCTYPE HTML Publ ...

  • Related Article

    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.