PHP file upload code details-PHP source code

Source: Internet
Author: User
Ec (2); Note: whether the file upload is successful depends on whether your written file directory is available, whether the form and post get the same name, and php tutorial. the file size uploaded in ini is not within the writable range. & Lt; html & gt; & lt; body & gt; & lt; formaction & quot; upload_file.php & quot; method & quot; post & quot; enctype & quot; multipa script ec (2); script

Note: whether the file upload is successful depends on whether your written file directory is available, whether the name obtained from the form and post is the same, and whether the size of the uploaded file in php tutorial. ini is within your writable range.



Php code.


$ Store_dir = 'd: upload; // path of the file stored on the server after being uploaded
$ Uploadfile = "$ store_dir". basename ($ _ files ['sendfile'] ['name']); // Original name of the uploaded file
$ Uploadfile_tmp = $ _ files ['sendfile'] ['name _ tmp ']; // temporary name of the uploaded file
$ Err_msg = $ _ files ['sendfile'] ['error']; // error message generated during File Upload

If ($ err_msg ){
Print "error code: $ err_msg
";
}
If (! Is_writeable ($ store_dir) {// check whether the uploaded file is writable.
Print "$ store_dir directory cannot write n ";
Exit;
}
Else {
Print "$ store_dir directory can be written to n ";
}

If (isset ($ _ files ['sendfile']) {
If (is_uploaded_file ($ uploadfile_tmp) {// check whether the uploaded file exists. if so, perform the next step.
Print "file verification succeeded n ";
}
Else {
Print "An error occurred while checking the file. This vulnerability may cause File Upload attacks! ";
Exit;
}
If (move_uploaded_file ($ uploadfile_tmp, $ uploadfile) {// rename the uploaded legal file and move it to the upload folder on the server
Print "File Uploaded successfully n ";
}
Else {
Print "failed to move the file, which may be under file upload attack! ";
Exit;
}
Print "the file has been uploaded!
";
}
Else {
Print "File Upload Failed!
";
}
?>

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.