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

Source: Internet
Author: User
Tags error code file upload html form php file

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: Copy code

<Html>
<Body>

<Form action = "upload_file.php" method = "post" enctype = "multipart/form-data">
<Label for = "attach_file"> Filename: </label>
<Input type = "file" name = "attach_file" id = "attach_file"/>
<Br/>
<Input type = "submit" name = "submit" value = "Submit"/>
</Form>

</Body>
</Html>

Pay attention to the following information about this form:

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

Create Upload script
The "upload_file.php" file contains the code for uploading files:

The code is as follows: Copy code


<? Php
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"];
}
?>

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: Copy code

<? Php

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"]. "<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 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: Copy code
<? Php
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"]. "<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 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, make sure that the following line is in the <form> file to be uploaded.

<Input type = "hidden" name = "MAX_FILE_SIZE" value = "500000">

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: Copy code

<? Php
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: Copy code

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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.