PHP upload file to server source code

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags allow users code copy creating creating a data echo error

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

Take a look at this HTML form for uploading files:

<html> <body> <form action= "upload_file.php" method= "post" enctype= "Multipart/form-data" > < Label for= "File" >Filename:</label> <input type= "file" name= "file" id= "file"/> <br/> <input Type= "Submit" name= "submit" value= "Submit"/> </form> </body> </html>

Please note the following information about this form:

The Enctype property of the <form> label stipulates what type of content to use when submitting the form. Use "Multipart/form-data" when the form requires binary data, such as the contents of the file.

The type= "file" attribute of the <input> label stipulates that input should be treated as a file. For example, when you preview in a browser, you see a navigation button next to the input box.

Note: Allowing users to upload files is a huge security risk. Only allow trusted users to perform file upload operations.

Create upload Script

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

&lt;?php if ($_files["file"] ["error"] &gt; 0) {echo ' ERROR: '. $_files["File" ["Error"]. "&lt;br/&gt;"; else {echo "Upload:". $_files["File" ["Name"]. "&lt;br/&gt;"; echo "Type:". $_files["File" ["type"]. "&lt;br/&gt;"; echo "Size:". ($_files["File"] ["size"]/1024). "Kb&lt;br/&gt;"; echo "Stored in:". $_files["File" ["Tmp_name"]; }?&gt;

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 subscript can be "name", "type", "Size", "tmp_name", or "error". Like this:

$_files["File" ["Name"]-the name of the uploaded file $_files["file" ["type"]-the type of file being uploaded $_files["file" ["Size"]-the size of the file being uploaded, 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. For security reasons, you should increase the restrictions on what users have permission to upload files.

Upload Limit

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

&lt;?php if (($_files["File" ["type"] = = "Image/gif") | | ($_files["File"] ["type"] = = "jpeg") | | ($_files["File"] ["type"] = = "Image/pjpeg") &amp;&amp; ($_files["file"] ["size"] &lt; 20000) {if ($_files["file"] ["error"] &gt; 0) {echo ' ERROR: '. $_files["File" ["Error"]. "&lt;br/&gt;"; else {echo "Upload:". $_files["File" ["Name"]. "&lt;br/&gt;"; echo "Type:". $_files["File" ["type"]. "&lt;br/&gt;"; echo "Size:". ($_files["File"] ["size"]/1024). "Kb&lt;br/&gt;"; echo "Stored in:". $_files["File" ["Tmp_name"]; } else {echo "Invalid file";}?&gt;

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

Save uploaded files

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

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

&lt;?php if (($_files["File" ["type"] = = "Image/gif") | | ($_files["File"] ["type"] = = "jpeg") | | ($_files["File"] ["type"] = = "Image/pjpeg") &amp;&amp; ($_files["file"] ["size"] &lt; 20000) {if ($_files["file"] ["error"] &gt; 0) {echo "Return Code:". $_files["File" ["Error"]. "&lt;br/&gt;"; else {echo "Upload:". $_files["File" ["Name"]. "&lt;br/&gt;"; echo "Type:". $_files["File" ["type"]. "&lt;br/&gt;"; echo "Size:". ($_files["File"] ["size"]/1024). "Kb&lt;br/&gt;"; echo "Temp file:". $_files["File" ["Tmp_name"]. "&lt;br/&gt;"; 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";}?&gt;

The above script 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".

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.