PHP file upload details

Source: Internet
Author: User
Tags php server php file upload jquery file upload

1. to upload a file, you must first configure it as needed in PHP. ini.

There are several important configuration sheets:

Option

Default Value

Description

Post_max_size

8 m

Controls the maximum size of post requests. It must be greater than the value of the upload_max_filesize option.

Max_input_time

60

Specifies the time that a POST request can take to submit all data. In seconds. End data submission after this time.

Memory_limit

128 m

The maximum memory that can be consumed on a script page.

Max_execution_time

30

Maximum script execution time. In seconds.

File_uploads

On

Specifies whether file upload is allowed. The default value is on.

Upload_max_filesize

2 M

Controls the maximum file size accepted by PHP. If the file is too large, PHP writes a 0-byte placeholder file.

Upload_tmp_dir

NULL

Must be set as a valid directory. You can put the uploaded file here for processing.

1. General PHP file upload, unless the file is small. It is like a 5 MB file, it may take more than one minute to complete the upload.
However, in PHP, the longest execution time of the page is 30 seconds by default. That is to say, the script is stopped after 30 seconds.
This causes the web page to fail to be opened. In this case, we can modify max_execution_time.
Search in PHP. ini
Max_execution_time
The default value is 30 seconds.
Max_execution_time = 0 0 indicates no limit
The above modified the timeout time for script execution in PHP file uploading.


2. Modify post_max_size to set the maximum size allowed by post data. This setting also affects PHP file upload.
The default post_max_size of PHP is 2 MB. If the size of post data is larger than that of post_max_size $ _ post and $ _ FILES superglobals, It is null.
Change to post_max_size.
Post_max_size = 150 m


3. Many people will change the second step. But the maximum size of PHP files uploaded is 8 Mb.
Why? We need to change the parameter upload_max_filesize to indicate the maximum size of the uploaded file.
Find upload_max_filesize. The default value is 8 Mb.
Upload_max_filesize = 100 m
In addition, it should be noted that in the PHP file upload, post_max_size is better than upload_max_filesize.

2. Set upload in HTML form

Note the following three points:

  1. Set Form submission method to post
  2. Add a <input> label of the "file" type (if multiple files are uploaded, add multiple)
  3. Add the enctype attribute to the form to use the new multipart/form-data MIME type.

After the data is submitted, the server accesses the submitted file through the super Global Array $ _ FILES. The number of elements contained in this array is the same as the number of form files. For example, if there is an upload file selection box in the form:

<Input type = "file" name = "filename">

After data is submitted, you can use $ _ FILES ['filename'] to obtain information about the file. The information is as follows:

$ _ FILES ['filename']:

['Name'] => file name

['Type'] => Object MIME type. Such as image/jpeg, text/plain,
Application/octet-stream.

['Tmp _ name'] => If the file size is smaller than the size of the file to be uploaded, this location indicates the temporary storage path of the uploaded file and the temporary file name, which are placed in the position specified by upload_tmp_dir.

['Error'] => error code.

['SIZE'] => file size.

The possible values of the error Code are as follows:

Encoding

Value

Description

UPLOAD_ERR_ OK

0

File Uploaded

UPLOAD_ERR_INI_SIZE

1

The file size is larger than the value specified by upload_max_filesize in php. ini.

UPLOAD_ERR_FORM_SIZE

2

The file size is greater than the value specified by MAX_FILE_SIZE of the form.

UPLOAD_ERR_PARTIAL

3

Incomplete file upload (may be terminated due to a long request time)

UPLOAD_ERR_NO_FILE

4

No file is uploaded with this request

UPLOAD_ERR_NO_TMP_DIR

6

No Temporary Folder specified in php. ini

The file processing should continue only when the value of $ _ FILES ['filename'] ['error'] is 0.

Example of HTML structure for uploading a single file:

<form action="" method="post" enctype="multipart/form-data"><p>File:<input type="file" name="filename" /><input type="submit" value="Send" /></p></form>

Multifile upload HTML structure example:

<form action="" method="post" enctype="multipart/form-data"><p>Files:<input type="file" name="filename[]" /><input type="file" name="filename[]" /><input type="file" name="filename[]" /><input type="submit" value="Send" /></p></form>

Iii. php server processing data

Operations such as file size and type restrictions, directory storage, and file information storage can be processed as needed. Here is a simple example:

 

Include_once 'conn/conn. php '; $ filename = $ _ FILES ['filename'] ['name']; // read the Upload File name, coexistence: array $ filetype =$ _ POST ['foundtype']; // read the uploaded file category $ tmpname = $ _ FILES ['filename'] ['tmp _ name']; // read the temporary file name, coexistence: array $ tmpsize = $ _ FILES ['filename'] ['SIZE']; // size of the uploaded file $ tmppub = $ _ POST ['ispub']; // whether to publish $ file_path = 'upfile/'; $ max = 0; if (! Is_null ($ tmpsize) {foreach ($ tmpsize as $ value) {$ max + = $ value;} if ($ max> 100000000 or $ max <= 0) {echo '<script> alert ("the total size of the uploaded file is greater than 100 mb. Please reselect 1"); location = "'. $ _ SERVER ['HTTP _ referer']. '"; </script>' ;}} else {/* var_dump ($ tmpsize); exit (); */echo '<script> alert (" File Upload error, check the program again "); location = "'. $ _ SERVER ['HTTP _ referer']. '"; </script>' ;}$ chkdownnum =''; for ($ I = 0; $ I <15; $ I ++) {$ chkdownnum. = dechex (rand (0, 15);} ($ I = 0; $ I <count ($ filename); $ I ++) {// upload all multiple files to move_uploaded_file through a loop ($ tmpname [$ I], $ file_path. $ filename [$ I]); // Add to database $ insertsql = 'insert into tb_upfile (filename, filepath, filetype, upauthor, chkdownnum, ispub) values ("'. trim ($ filename [$ I]). '","'. $ file_path. $ filename [$ I]. '","'. $ filetype [$ I]. '","'. $ _ SESSION ['name']. '","'. trim ($ chkdownnum ). '",'. $ tmppub [$ I]. ')'; $ conne-> uidRst ($ insertsql);} echo '<div Style = "padding-top: 30px;"> the file is uploaded successfully! </Div> ';

Here we recommend several very useful jquery plug-ins: uploadify (Flash/HTML5) jquery File Upload (HTML5) plupload (multiple runtime support)


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.