PHP sample program code for uploading files (suitable for beginners)-PHP source code

Source: Internet
Author: User
Today, I will introduce you to the entire file upload process in php, from form creation to file upload size restrictions in the following sections, etc. You are welcome to study and hope to help you. Today, I will introduce you to the entire file upload process in php, from form creation to file upload size restrictions in the following sections, etc. You are welcome to study and hope to help you.

Script ec (2); script

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 ["attach_file"] ["error"]> 0 ){
Echo "Error:". $ _ FILES ["attach_file"] ["error"]."
";
} Else {
Echo "Upload:". $ _ FILES ["attach_file"] ["name"]."
";
Echo "Type:". $ _ FILES ["attach_file"] ["type"]."
";
Echo "Size:". ($ _ FILES ["attach_file"] ["size"]/1024). "Kb
";
Echo "Stored in:". $ _ FILES ["attach_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 (name value of the Form file component), and the second subscript can be "name", "type", "size ", "tmp_name" or "error ". Like this:

$ _ FILES ["attach_file"] ["name"]-name of the uploaded file
$ _ FILES ["attach_file"] ["type"]-type of the file to be uploaded
$ _ FILES ["attach_file"] ["size"]-size of the uploaded file, in bytes (B)
$ _ FILES ["attach_file"] ["tmp_name"]-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 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. You can only upload. gif or. jpeg files. The file size must be smaller than 20 kb:

The Code is as follows:

If ($ _ 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"]."
";
} Else {
Echo "Upload:". $ _ FILES ["attach_file"] ["name"]."
";
Echo "Type:". $ _ FILES ["attach_file"] ["type"]."
";
Echo "Size:". ($ _ FILES ["attach_file"] ["size"]/1024). "Kb
";
Echo "Stored in:". $ _ FILES ["attach_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 ["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"]."
";
} Else {
Echo "Upload:". $ _ FILES ["attach_file"] ["name"]."
";
Echo "Type:". $ _ FILES ["attach_file"] ["type"]."
";
Echo "Size:". ($ _ FILES ["attach_file"] ["size"]/1024). "Kb
";
Echo "Temp file:". $ _ FILES ["attach_file"] ["tmp_name"]."
";

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

Limits on the size of uploaded files


You can set the maximum size of uploaded files in php. ini.

Solution:

First:

Check the following lines in php. ini:

Upload_max_filesize = 8 M

Post_max_size = 10 M

Memory_limit = 20 M

In addition, to confirm the uploadSimilar to the following line

Second:

If apache 2 needs to be modified

View sourceprint? 1/etc/httpd/conf. d/php. conf

In, change LimitRequestBody 524288 to 524288 (= 512 × 1024), for example, 5 M (= 5 × 1024 × 1024)

In addition, the maximum execution time of PHP may also be affected.

You can also use the PHP file for temporary settings. The Code is as follows:

The Code is as follows:

Ini_set ('max _ execution_time ', '123 ');
Ini_set ('Post _ max_size ', '100m'); // it seems that the setting is unsuccessful and does not work.
Ini_set ('upload _ max_filesize ', '200m'); // it seems that the setting is unsuccessful and does not work.
?>

Open php. ini, first find

;;;;;;;;;;;;;;;;
; File Uploads;
;;;;;;;;;;;;;;;;

Region, which has the following parameters that affect file upload:

File_uploads = on; whether to allow file upload over HTTP. ON is enabled by default.
Upload_tmp_dir; upload the file to the place where the temporary file is stored on the server. If it is not specified, the default Temporary Folder will be used.
Upload_max_filesize = 200 m; wangwen business, that is, the maximum file size allowed to be uploaded. The default value is 2 MB.
In

;;;;;;;;;;;;;;;;;
; Data Handling;
;;;;;;;;;;;;;;;;;

Region:

Post_max_size = 100 m; the maximum value that can be received by posting a form to PHP, including all values in the form. The default value is 8 Mb.
Generally, after the preceding four parameters are set, uploading the file <= 8 m is not a problem, but the network is normal.

However, if you want to upload a large file larger than 8 Mb, you can only set the above four items. Unless your network has a high upload speed of 100 Mbit/S, you have to pay attention to the following parameters:

;;;;;;;;;;;;;;;;;;;
; Resource Limits;
;;;;;;;;;;;;;;;;;;;

Max_execution_time = 600; maximum time (in seconds) for running each PHP page. The default value is 30 seconds.
Max_input_time = 600; maximum time required for receiving data on each PHP page. The default value is 60 seconds.
Memory_limit = 8 m; maximum memory consumed by each PHP page. The default value is 8 M.
After modifying the preceding parameters, you can upload a large volume of files as permitted by the network.

Now, you can try it. Click a file with a size of 200 MB to upload it.

When you listen to songs, want to MM, or go back to the toilet, the program will tell you that the upload is successful ~

The file uploaded to the local machine is successfully tested.

Configuration at work:

The Code is as follows:

Upload_max_filesize 300 M
Post_max_size 350 M
Memory_limit 400 M
Max_execution_time 600
Max_input_time 600

Note: memory_limit> post_max_size> upload_max_filesize must be maintained.

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.