PHP upload (single) file example

Source: Internet
Author: User

With PHP, you can upload files to the server.

Create a File Upload form

It is very useful to allow users to upload files from a form.

Take a look at the following HTML form for uploading files:

Please note the following information about this form:

The enctype attribute of the <form> tag specifies what type of content to use when submitting a form. Use "multipart/form-data" When the form requires binary data, such as the contents of a file.

The type= "file" attribute of the <input> 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 file upload operations.

Create an upload script

The "upload_file.php" file contains the code for uploading the file:

<?phpif ($_files["Attach_file" ["Error"] > 0) {echo "Error:". $_files["Attach_file" ["Error"]. "<br/>";} Else{echo "Upload:". $_files["Attach_file" ["Name"]. "<br/>"; echo "Type:". $_files["Attach_file" ["type"]. "<br/>"; echo "Size:". ($_files["Attach_file" ["size"]/1024). "Kb<br/>"; echo "Stored in:". $_files["Attach_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 ( the name value of the form file component ), and the second subscript can be "name", "type", "Size", "Tmp_name" or "error". Just like this:

    • $_files["Attach_file" ["Name"]-the name of the file being uploaded
    • $_files["Attach_file" ["type"]-the type of file being uploaded
    • $_files["Attach_file" ["Size"]-the size of the uploaded file, in bytes (b)
    • $_files["Attach_file" ["Tmp_name"]-the name of the temporary copy of the file stored on the server
    • $_files["Attach_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.

Upload limit

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:

<?phpif (($_files["Attach_file" ["type"] = = "Image/gif") | | ($_files["Attach_file" ["type"] = = "Image/jpeg") | | ($_files["Attach_file" ["type"] = = "Image/pjpeg")) && ($_files["Attach_file" ["Size"] < 20000)) {if ($_files["Attach_file" ["Error"] > 0) {echo "Error:". $_files["Attach_file" ["Error"]. "<br/>";} Else{echo "Upload:". $_files["Attach_file" ["Name"]. "<br/>"; echo "Type:". $_files["Attach_file" ["type"]. "<br/>"; echo "Size:". ($_files["Attach_file" ["size"]/1024). "Kb<br/>"; echo "Stored in:". $_files["Attach_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.

Save the uploaded file

The above example creates a temporary copy of the uploaded file in the server's PHP Temp folder.

This temporary copy of the file disappears at the end of the script. To save the uploaded file, we need to copy it to another location:

<?phpif ((($_files["Attach_file" ["type"] = = "Image/gif") | | ($_files["Attach_file" ["type"] = = "Image/jpeg") | | ($_files["Attach_file" ["type"] = = "Image/pjpeg")) && ($_files["Attach_file" ["Size"] < 20000)) {if ($_files["Attach_file" ["Error"] > 0) {echo "Return Code: " . $_files["Attach_file" ["Error"]. "<br/>";} Else{echo "Upload:". $_files["Attach_file" ["Name"]. "<br/>"; echo "Type:". $_files["Attach_file" ["type"]. "<br/>"; echo "Size:". ($_files["Attach_file" ["size"]/1024). "Kb<br/>"; echo "Temp file:". $_files["Attach_file" ["Tmp_name"]. "<br/>", if (file_exists ("upload/". $_files["Attach_file" ["Name"])) {echo $_files["Attach_file" ["Name"]. "already exists.";} Else{move_uploaded_file ($_files["Attach_file" ["Tmp_name"], "upload/". $_files["Attach_file" ["name"]); echo " Stored in: "." Upload/". $_files["Attach_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".

Extended reading :

PHP simultaneously uploads "multiple" file examples and formats $_files array information

"Recommended" PHP upload file size limit Daquan

PHP upload (single) file example

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.