PHP Set picture file upload size of the concrete implementation method _php Example

Source: Internet
Author: User
Tags file upload memory usage php file upload time limit

We briefly introduce some of the parameters involved in PHP file upload:

file_uploads: Allow the switch to upload files via HTTP, which is on by default.
Upload_tmp_dir:upload_tmp_dir used to describe the temporary directory of files placed in PHP upload, to upload files, you have to ensure that the server does not turn off temporary files and have write permissions on the folder, if not specified then PHP use the system default values.
upload_max_filesize: The maximum size allowed for uploading files, the default is 2M.
post_max_size: Controls the maximum amount of data that PHP can receive when using the Post method for a form submission. If you want to use the php file upload feature, you need to change this value to be larger than upload_max_filesize.
Max_input_time: Limits the time to receive data by post, get, and put in seconds. If your application is running on a low speed link, you need to increase this value to accommodate the additional time required to receive data.
Memory_limit: To avoid running scripts that use the system's available memory heavily, PHP allows you to define memory usage limits. The value of the maximum memory capacity variable Memory_limit that a single script program can use is specified by the Memory_limit variable to be appropriately larger than the Post_max_size value.
Max_execution_time:max_execution_time Sets the time, in seconds, before PHP waits for the script to finish executing before the script is forced to terminate. This variable is useful when the script enters an infinite loop state. However, when there is a legitimate activity that takes a long time to complete (such as uploading a large file), this feature can also cause the operation to fail. In such cases you must consider increasing the value of this variable to avoid PHP shutting down the script while the script is performing some important process.
For Linux hosts, there may be php.conf files underneath the/etc/httpd/conf.d/access.conf/, which may solve some of the system's file size limitations.

PHP upload to add the following code, you can temporarily let PHP upload large files, as follows:

Copy Code code as follows:

<?php

HTTP upload file switch, default to on that is open

Ini_set (' file_uploads ', ' on ');

The time limit for receiving data through post, get, and put is limited to 90 seconds default value: 60

Ini_set (' Max_input_time ', ' 90 ');

The script execution time is changed from the default 30 seconds to 180 seconds.

Ini_set (' max_execution_time ', ' 180 ');

The post variable is modified from 2M to 8M, and this value is changed to be larger than upload_max_filesize

Ini_set (' post_max_size ', ' 12M ');

Upload file modification is also 8M, and the above this somewhat related to the size of the relationship.

Ini_set (' upload_max_filesize ', ' 10M ');

The script that is running massively uses the system usable memory, uploads the picture to more points, the best is 1.5 times times bigger than post_max_size

Ini_set (' Memory_limit ', ' 20M ');

?>

To see if the above variables have been modified successfully:

Copy Code code as follows:

<?php

echo ini_get (' File_uploads '). " \ n ";

echo ini_get (' Max_input_time '). " \ n ";

echo ini_get (' Max_execution_time '). " \ n ";

echo ini_get (' Post_max_size '). " \ n ";

echo ini_get (' upload_max_filesize '). " \ n ";

echo ini_get (' Memory_limit '). " \ n ";

?>

Modified in php.ini, involving a size constraint relationship of three values. So is a temporary solution, this why can not be modified, PHP is the mode of operation is a great relationship, such as Safe mode.

Ini_set directives cannot be used in Safe mode: Max_execution_time, Memory_limit, Child_terminate.

Therefore, the following method, like Post_max_size,upload_max_filesize, cannot be modified:

Copy Code code as follows:

Ini_set (' post_max_size ', ' 10M ');

Ini_set (' upload_max_filesize ', ' 8M ');

The correct approach is to use the. htaccess file:

Copy Code code as follows:

Php_value upload_max_filesize 8M

Php_value post_max_size 10M

The premise is that the site is configured in httpd.conf: allowoverride all.

In the PHP document there is a description of the Upload_max_filesize modifiable range is php_ini_perdir. Php_ini_perdir means that the instructions in the domain can be modified in php.ini, httpd.conf, or. htaccess files. Php_ini_system directives can be modified in php.ini and httpd.conf files. So upload_max_filesize can not be modified with Int_set. Only the modifiable range is Php_ini_all can be modified with Int_set.

method to get the value:

Copy Code code as follows:

if (@ini_get ("File_uploads")) {

$arrInfo [' fileupload '] = "Allow-file". Ini_get ("Upload_max_filesize"). "-Form:". Ini_get ("Post_max_size");

}

else {

$arrInfo [' fileupload '] = "<font color= ' red ' > Forbidden </font>";

}

if (Get_cfg_var (' register_globals ')) {

$arrInfo [' onoff '] = "open";

}else{

$arrInfo [' onoff '] = "close";

}

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.