PHP Image File Upload implementation code

Source: Internet
Author: User
With PHP, you can upload files to the server. Some image judgments are added. If the file type is not determined, files in any format can be uploaded.

With PHP, you can upload files to the server. Some image judgments are added. If the file type is not determined, files in any format can be uploaded.

For the sake of website security, PHP files will not be uploaded. If someone enters your background and uploads a PHP file, all the source code of your website will be saved to him, directly package your code. Therefore, you must control the upload directory and file type. Generally, you can only upload images.

Create a file upload form
It is very useful to allow users to upload files from forms.
See the following HTML form for uploading files:

The Code is as follows:








Pay attention to the following information about this form:
The enctype attribute of a tag specifies the content type to be used when submitting a form. When a form requires binary data, such as file content, use multipart/form-data ".
The type = "file" attribute of the tag specifies that the input should be processed as a file. For example, when previewing in a browser, a browser button is displayed next to the input box.
Note: allowing users to upload files is a huge security risk. Only trusted users are allowed to upload files.
Create upload script
The "upload_file.php" file contains the code for uploading files:

The Code is as follows:


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). "Kb
";
Echo "Stored in:". $ _ FILES ["file"] ["tmp_name"];
}
?>


By using the Global Array $ _ FILES of PHP, you can upload FILES from the 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:

The Code is as follows:


$ _ FILES ["file"] ["name"]-name of the uploaded file
$ _ FILES ["file"] ["type"]-type of the file to be uploaded
$ _ FILES ["file"] ["size"]-size of the uploaded file, in bytes
$ _ FILES ["file"] ["tmp_name"]-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 File Upload method. Based on security considerations, you should add restrictions on which users have the right to upload files.
Upload restrictions
In this script, we have added restrictions on file upload on the Hong Kong virtual host. You can only upload. gif or. jpeg files on a Hong Kong VM. The file size must be smaller than 20 kb:

The Code is as follows:


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). "Kb
";
Echo "Stored in:". $ _ FILES ["file"] ["tmp_name"];
}
}
Else
{
Echo "Invalid file ";
}
?>


Note: For IE, the jpg file type must be pjpeg and for FireFox, it must be jpeg.
Save the uploaded file
In the above example, a temporary copy of the uploaded file is created in the PHP temporary folder on the server.
The temporary copy file will disappear at the end of the script. To save the uploaded file, we need to copy it to another location:

The Code is as follows:


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). "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 above script checks whether the file already exists on the Hong Kong server. If it does not exist, copy the file to the specified folder.
Note: In this example, the file is saved to a new folder named "upload.

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.