如何根据每个脚本设置POST大小限制?

Sca*_*ich 4 php post max limit

ETA:有人建议,ini_set()但我的理解是这是服务器端设置; 我需要在运行时具有每个脚本的独立性.例如,三个脚本同时运行如下:x.php使用2k,y.php使用4k,z.php使用8k.

如何以编程方式在每个脚本的基础上设置POST最大大小限制(即,不是在服务器范围内)?

例如在Perl中,只需在每个脚本中执行此操作,从而允许每个脚本在运行时设置自己的限制:

# The $POST_MAX variable also applies to the combined size of all the elements
# in a form. Therefore, you can set this variable to keep people from pasting
# huge amounts of junk into text fields, too.
$CGI::POST_MAX = 1024 * 2;
Run Code Online (Sandbox Code Playgroud)

我意识到php.ini中有一个设置来限制所有 PHP脚本的最大POST大小:

; Maximum size of POST data that PHP will accept.
; http://php.net/post-max-size
post_max_size = 8M
Run Code Online (Sandbox Code Playgroud)

但我想知道如何在每个脚本的基础上执行此操作(因为每个脚本都有自己的限制).

注意:大概可以在Apache httpd.conf和/或.htaccess中设置限制,但我不想这样做.

Dip*_*mar 8

使用ini_set,然后根据需要设置您的PHP设置.

ini_set('upload_max_filesize', '60M');     
ini_set('max_execution_time', '999');
ini_set('memory_limit', '128M');
ini_set('post_max_size', '60M'); 
Run Code Online (Sandbox Code Playgroud)

  • ini_set()将仅影响当前脚本运行时,覆盖服务器范围的设置; 因此,您可以逐个脚本地设置不同的值. (3认同)
  • 无法使用ini_set设置post_max_size.http://php.net/manual/en/ini.list.php (2认同)
  • `ini_set()` 不再适用于 `post_max_size` 或 `upload_max_filesize` (截至 2020 年 12 月),因为它们在 PHP 中设置为 ***PHP_INI_PERDIR*** - 我们只能在 `ini` 中更改它,` httpd.conf` 或 `.htaccess` - [这里是线程](/sf/ask/940958931/) (2认同)