PHP File Upload Form ~ ~ Learning Notes _php Tutorial

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

PHP File Upload Form ~ ~ Learn Notes


PHP File Upload
With PHP, you can upload files to the server.
--------------------------------------------------------------------------------------------------------------- ----
Create a File Upload form: It is useful to allow users to upload files from a form;

The following is an HTML form for uploading a file:


The enctype attribute of the label specifies the type of content to be used when the form is submitted. Use "Multipart/form-data" when the form requires binary data, such as the contents of a file.
The type= "file" attribute of the tag specifies that the input should be processed as a file. For example, when previewing in a browser, you'll see a browse button next to the input box.

Note: Allowing users to upload files is a huge security risk. Please allow only trusted users to perform actions on the file.
--------------------------------------------------------------------------------------------------------------- ----
To Create an upload script:
The "upload_file.php" file contains the code for uploading the file:

 
   if ($_files["file" ["error"] > 0) {echo "Upload error:". $_files["File" ["Error"]. "
"; }else {echo "Upload:". $_files["File" ["Name"]. "
"; echo "Type:". $_files["File" ["type"]. "
"; echo "Size:". $_files["File" ["Size"]/1024. The KB
"; echo "Store in:". $_files["File" ["Tmp_name"]. "
" ; }?>

By using PHP's global array $_files, you can upload files from a client computer to a remote server.

The first parameter is the input name of the form, and the second table can be: "Name", "type", "Size", "Tmp_name" or "error". Just like this:
$_files["File" ["Name"]-the name of the file being uploaded
$_files["File" ["type"]-the type of file being uploaded
$_files["File" ["Size"]-the size of the uploaded file, measured in bytes
$_files["File" ["Tmp_name"]-the name of the temporary copy of the file stored on the server
$_files["File" ["Error"]-error code caused by file upload

This is a very simple way to upload files. Based on security considerations, you should increase the restrictions on what users have permission to upload files.

In this script, we have added restrictions on file uploads. Users can only upload. gif or. jpeg files, and the file size must be less than KB:

  if (($_files["File" ["type"] = = "Image/gif")|| ($_files["File" ["type"] = = "Image/jpeg")|| ($_files["File" ["type"] = = "Image/pjpeg"))&& ($_files["File" ["Size"] < 20000)) {if ($_files["file" ["error"] > 0) {echo "Error:". $_files["File" ["Error"]. "
";}else {echo "Upload:". $_files["File" ["Name"]. "
";echo "Type:". $_files["File" ["type"]. "
";echo "Size:". ($_files["File" ["Size"]/1024). The Kb
";echo "Stored in:". $_files["File" ["Tmp_name"];}}else {echo "Invalid file";}?>

Note: For IE, the type of the recognized JPG file must be pjpeg, and for FireFox, it must be JPEG.

  if (($_files["File" ["type"] = = "Image/gif")|| ($_files["File" ["type"] = = "Image/jpeg")|| ($_files["File" ["type"] = = "Image/pjpeg"))&& ($_files["File" ["Size"] < 20000)) {if ($_files["file" ["error"] > 0) {echo "Return Code:". $_files["File" ["Error"]. "
";}else {echo "Upload:". $_files["File" ["Name"]. "
";echo "Type:". $_files["File" ["type"]. "
";echo "Size:". ($_files["File" ["Size"]/1024). The Kb
";echo "Temp file:". $_files["File" ["Tmp_name"]. "
";if (file_exists ("upload/". $_files["File" ["Name"])) {echo $_files["File" ["Name"]. "already exists.";}else {Move_uploaded_file ($_files["file"] ["Tmp_name"],"Upload/". $_files["File" ["name"]);echo "Stored in:". " Upload/". $_files["File" ["Name"];}}}else {echo "Invalid file";}?>

The script above detects if the file already exists, and if it does not, copies the file to the specified folder.

Note: This example saves the file to a new folder named "Upload".

http://www.bkjia.com/PHPjc/969604.html www.bkjia.com true http://www.bkjia.com/PHPjc/969604.html techarticle php File Upload form ~ ~ Learning notes php file upload via PHP, you can upload files to the server. -----------------------------------------------------------------------------...

  • 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.