PHP File Upload form ~~ Study Notes,

Source: Internet
Author: User
Tags php file upload

PHP File Upload form ~~ Study Notes,
PHP File Upload
With PHP, you can upload files to the server.
Bytes -------------------------------------------------------------------------------------------------------------------
Create a file upload form: it is very useful for users to upload files from the form;

The following is an html form for uploading files:


<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 ".
<Input> 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 perform operations on files.
Bytes -------------------------------------------------------------------------------------------------------------------
Create an upload script:
The "upload_file.php" file contains the code for uploading files:

<?php<span style="white-space:pre"></span>if($_FILES["file"]["error"] > 0) {<span style="white-space:pre"></span>echo "Upload Error: ". $_FILES["file"]["error"] . "<br />";<span style="white-space:pre"></span>}else {<span style="white-space:pre"></span>echo "Upload : ". $_FILES["file"]["name"] . "<br />";<span style="white-space:pre"></span>echo "Type : ". $_FILES["file"]["type"] . "<br />";<span style="white-space:pre"></span>echo "Size : ". $_FILES["file"]["size"]/1024 . " kb<br />";<span style="white-space:pre"></span>echo "Store in : ". $_FILES["file"]["tmp_name"] . "<br />" ;<span style="white-space:pre"></span>}?>

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 table can be: "name", "type", "size", "tmp_name" or "error". just like this:
$ _ 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.

In this script, we have added restrictions on file upload. You can only upload. gif or. jpeg files. The file size must be smaller than 20 kb:

<?php<span style="white-space:pre"></span>if ((($_FILES["file"]["type"] == "image/gif")<span style="white-space:pre"></span>|| ($_FILES["file"]["type"] == "image/jpeg")<span style="white-space:pre"></span>|| ($_FILES["file"]["type"] == "image/pjpeg"))<span style="white-space:pre"></span>&& ($_FILES["file"]["size"] < 20000)) {<span style="white-space:pre"></span>if ($_FILES["file"]["error"] > 0) {<span style="white-space:pre"></span>echo "Error: " . $_FILES["file"]["error"] . "<br />";<span style="white-space:pre"></span>}else {<span style="white-space:pre"></span>echo "Upload: " . $_FILES["file"]["name"] . "<br />";<span style="white-space:pre"></span>echo "Type: " . $_FILES["file"]["type"] . "<br />";<span style="white-space:pre"></span>echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";<span style="white-space:pre"></span>echo "Stored in: " . $_FILES["file"]["tmp_name"];<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}else {<span style="white-space:pre"></span>echo "Invalid file";<span style="white-space:pre"></span>}?>

Note: For IE, the jpg file type must be pjpeg and for FireFox, it must be jpeg.

<?php<span style="white-space:pre"></span>if ((($_FILES["file"]["type"] == "image/gif")<span style="white-space:pre"></span>|| ($_FILES["file"]["type"] == "image/jpeg")<span style="white-space:pre"></span>|| ($_FILES["file"]["type"] == "image/pjpeg"))<span style="white-space:pre"></span>&& ($_FILES["file"]["size"] < 20000)) {<span style="white-space:pre"></span>if ($_FILES["file"]["error"] > 0) {<span style="white-space:pre"></span>echo "Return Code: " . $_FILES["file"]["error"] . "<br />";<span style="white-space:pre"></span>}else {<span style="white-space:pre"></span>echo "Upload: " . $_FILES["file"]["name"] . "<br />";<span style="white-space:pre"></span>echo "Type: " . $_FILES["file"]["type"] . "<br />";<span style="white-space:pre"></span>echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";<span style="white-space:pre"></span>echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";<span style="white-space:pre"></span>if (file_exists("upload/" . $_FILES["file"]["name"])) {<span style="white-space:pre"></span>echo $_FILES["file"]["name"] . " already exists. ";<span style="white-space:pre"></span>}else {<span style="white-space:pre"></span>move_uploaded_file($_FILES["file"]["tmp_name"],<span style="white-space:pre"></span>"upload/" . $_FILES["file"]["name"]);<span style="white-space:pre"></span>echo "Stored in: " . "upload/" . $_FILES["file"]["name"];<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}else {<span style="white-space:pre"></span>echo "Invalid file";<span style="white-space:pre"></span>}?>

The above script checks whether the file already exists. 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.