如何在php中动态更改最大上传文件限制?

问题描述:

I want to change the max upload file limit dynamically. means by not changing the php.ini file on server.
Whenever user had uploaded more then 2 mb then my upload limit should be change.
I want to do all this through php.

Thanks in advance.

我想动态更改最大上传文件限制。 意思是不更改服务器上的php.ini文件。
当用户上传的内容超过2 mb时,我的上传限制应该更改。
我想通过php完成所有这些。
p>

提前致谢。 p> div>

Try this:

ini_set('upload_max_filesize', your_value_here);

Make sure also that you have specified the correct acceptable settings for:

  • file_uploads
  • upload_max_filesize
  • max_input_time
  • memory_limit
  • max_execution_time
  • post_max_size

The limits enforces by upload_max_filesize are enforced before the php script is run, so the setting cannot be changed dynamically with ini_set.

For more information on the file upload ini settings and where they can be changed, see: - http://php.net/manual/en/ini.core.php#ini.sect.file-uploads - http://php.net/manual/en/configuration.changes.modes.php

Assuming by "user" you mean a visitor to your site, there are really only two methods you can enforce such a limit without the file reaching its final destination:

1) Before the upload has occurred: On the client side. You could definitely do this using a java-based uploader. Whether you can get the filesize of the selected file using javascript, I don't know.

2) After the file is uploaded to the server, but before you move it to the final destination (before you run move_uploaded_file)

If you can't modify your php.ini, you might be able to do it with a .htaccess file:

php_value upload_max_filesize 50M
php_value post_max_size 50M