Php getting started learning knowledge point 3 PHP Upload

Source: Internet
Author: User

I. HTML code for uploading forms
Copy codeThe Code is as follows:
<Form action = "UpLoad. php" method = "post" enctype = "multipart/form-data" name = "upFrm">
<Input type = "file" name = "Imgs" id = "Imgs">
<Input type = "submit" name = "subBtn" value = "Upload">
</Form>

Note: 1. action is a new upload
2. The enctype attribute must be written as "multipart/form-data"
Ii. PHP code
Copy codeThe Code is as follows:
<? Php
If (is_uploaded_file ($ _ FILES ["Imgs"] ["tmp_name"]) {
$ Phpupfile = $ _ FILES ["Imgs"];
// Output the Array Structure of the uploaded file;
Print_r ($ phpupfile );
// Output all types of information about the uploaded file
Echo $ phpupfile ["size"]. "<br>"; // file name
Echo $ phpupfile ["type"]. "<br>"; // file type
Echo $ phpupfile ["tmp_name"]. "<br>"; // the file name contains the path
Echo $ phpupfile ["name"]. "<br>"; // uploaded file name
/*
* Upload error message
* 0 indicates that the upload is successful,
* 1 or 2 indicates that the maximum upload value is exceeded.
* 3 indicates that only partial uploads are allowed.
* 4 indicates that no file is uploaded.
* 5 indicates that the size of the uploaded file is 0.
*/
Echo $ phpupfile ["error"]. "<br> ";
// Upload function (after the form is submitted, the uploaded file is saved in the temporary folder on the server. In this case, you need to move it to the specified folder on the website)
Move_uploaded_file ($ phpupfile ["tmp_name"], $ phpupfile ["name"]); // Save the uploaded file to the specified folder
/*
* The following are additional parts.
*/
// Determine whether the object exists. 1 indicates that the object exists, and 0 indicates that the object is not found.
Echo 'this File is exists: '. file_exists ($ phpupfile ["name"]).' <br> '; // query whether a File or directory exists
// Unlink
Echo 'delete file: '. unlink ($ phpupfile ["name"]).'; 1 indicates that deletion is successful, and 0 indicates deletion failed ';
// Create a folder using mkdir
If (file_exists ('pic ') = FALSE ){
Mkdir ("pic ");
}
If (file_exists ('pic/Ts') = FALSE ){
Mkdir ("pic/ts ");
}
// Rmdir delete a folder
If (file_exists ('pic/Ts') = FALSE ){
Rmdir ('pic/Ts ');
}
// Rename
Rename ("guitar11-hp-sprite.png", "1.png ");
Echo "<br> ";
Echo 'this File is exists: '. file_exists ($ phpupfile ["name"]).' <br> ';
}
?>

Note: 1. $ _ FILES ["Imgs"] The Imgs is the control name defined in your HTML code.

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.