PHP set image file upload size specific implementation method _php instance

Source: Internet
Author: User
Tags php file upload
Let's briefly introduce some of the parameters involved in uploading PHP files:

File_uploads: Whether to allow the switch to upload files over HTTP, the default is on is on.
Upload_tmp_dir:upload_tmp_dir used to explain the PHP upload files placed in a temporary directory, to upload files, you have to ensure that the server does not close the temporary files and have write permissions to the folder, if not specified, PHP uses the system default values.
upload_max_filesize: The maximum number of file sizes allowed to upload, default is 2M.
post_max_size: Controls the maximum amount of data PHP can receive in a single form submission using the Post method. If you want to use PHP file upload functionality, you need to change this value to be larger than upload_max_filesize.
Max_input_time: The time to receive data by post, get, and put is limited in seconds. If your application is running on a low-speed link, you need to increase this value to accommodate the additional time it takes to receive the data.
memory_limit: PHP allows you to define memory usage limits in order to avoid running scripts that use the system's available memory heavily. The Memory_limit variable is used to specify the maximum memory capacity variable that a single script can use memory_limit the value should be appropriately greater than the value of post_max_size.
Max_execution_time:max_execution_time sets the time at which PHP waits for the script to complete before forcing the script to terminate, which is calculated in seconds. This variable is useful when the script enters an infinite loop state. However, this feature also causes the operation to fail when there is a legitimate activity that takes a long time to complete, such as uploading large files. In such cases, you must consider increasing the value of this variable to avoid PHP closing the script when the script is performing some important process.
For Linux hosts, there may also be php.conf files under/etc/httpd/conf.d/access.conf/, which may solve some system file size limitation issues.

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

Copy the Code code as follows:

HTTP upload file switch, the default is on is open

Ini_set (' file_uploads ', ' on ');

The time to receive data via 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 of 30 seconds to 180 seconds.

Ini_set (' max_execution_time ', ' 180 ');

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

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

Upload file modification is also 8M, and the above this a little relationship, the size of unequal relationship.

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

Running scripts use a lot of system available memory, upload pictures to multipoint, preferably 1.5 times times larger than post_max_size

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

?>

See if the above variables were modified successfully:

Copy the Code code as follows:

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 scheme, why this can not be modified, there is a PHP operating mode is very much related, such as security mode.

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

Therefore, like post_max_size,upload_max_filesize with the following method is not modified:

Copy the 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 the 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 documentation, it is stated that the modifiable range of upload_max_filesize is php_ini_perdir. Php_ini_perdir means that intra-domain directives can be modified in php.ini, httpd.conf, or. htaccess files. Php_ini_system Intra-domain directives can be modified in the php.ini and httpd.conf files. So upload_max_filesize with Int_set is impossible to modify. Only the modifiable range is php_ini_all and can be modified with Int_set.

method to get the value:

Copy the 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 '] = "forbidden";

}

if (Get_cfg_var (' register_globals ')) {

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

}else{

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

}

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