PHP设置图片文件上传大小的具体实现方法_PHP
我们简要介绍一下PHP文件上传涉及到的一些参数:
•file_uploads :是否允许通过HTTP上传文件的开关,默认为ON即是开。
•upload_tmp_dir :upload_tmp_dir用来说明PHP上传的文件放置的临时目录,要想上传文件,得保证服务器没有关闭临时文件和有对文件夹的写权限,如果未指定则PHP使用系统默认值。
•upload_max_filesize :允许上传文件大小的最大值,默认为2M。
•post_max_size :控制在采用POST方法进行一次表单提交中PHP所能够接收的最大数据量。如果希望使用PHP文件上传功能,则需要将此值改为比upload_max_filesize要大。
•max_input_time :以秒为单位对通过POST、GET以及PUT方式接收数据时间进行限制。如果应用程序所运行环境处在低速链路上,则需要增加此值以适应接收数据所需的更多时间。
•memory_limit :为了避免正在运行的脚本大量使用系统可用内存,PHP允许定义内存使用限额。通过memory_limit变量来指定单个脚本程序可以使用的最大内存容量变量memory_limit的值应当适当大于post_max_size的值。
•max_execution_time :max_execution_time设置了在强制终止脚本前PHP等待脚本执行完毕的时间,此时间以秒计算。当脚本进入了一个无限循环状态时此变量非常有用。然而,当存在一个需要很长时间完成的合法活动时(例如上传大型文件),这项功能也会导致操作失败。在这样的情况下必须考虑将此变量值增加,以避免PHP在脚本正在执行某些重要过程的时候将脚本关闭。
对于linux主机,可能在/etc/httpd/conf.d/access.conf/下面里面还有php.conf 文件,这个文件可能会解决一些系统的文件大小限制问题。
在PHP上传上加入下面的代码,即可暂时让PHP能上传大文件,如下:
复制代码 代码如下:
//HTTP上传文件的开关,默认为ON即是开
ini_set('file_uploads','ON');
//通过POST、GET以及PUT方式接收数据时间进行限制为90秒 默认值:60
ini_set('max_input_time','90');
//脚本执行时间就由默认的30秒变为180秒
ini_set('max_execution_time', '180');
//Post变量由2M修改为8M,此值改为比upload_max_filesize要大
ini_set('post_max_size', '12M');
//上传文件修改也为8M,和上面这个有点关系,大小不等的关系。
ini_set('upload_max_filesize','10M');
//正在运行的脚本大量使用系统可用内存,上传图片给多点,最好比post_max_size大1.5倍
ini_set('memory_limit','20M');
?>
查看上面的变量是否修改成功:
复制代码 代码如下:
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";
?>
在php.ini中修改,涉及三个值的大小约束关系。所以是一种临时方案,这个为何不能修改,是有PHP的运行模式是有很大的关系的,如安全模式。
在安全模式下不能使用ini_set的指令:max_execution_time、memory_limit、child_terminate。
因此,像post_max_size,upload_max_filesize用下面的方法是修改不了的:
复制代码 代码如下:
ini_set('post_max_size','10M');
ini_set('upload_max_filesize','8M');
正确做法是用.htaccess文件:
复制代码 代码如下:
php_value upload_max_filesize 8M
php_value post_max_size 10M
前提是该站点在httpd.conf中配置了:AllowOverride All。
在php文档里有说明upload_max_filesize的可修改范围是PHP_INI_PERDIR。PHP_INI_PERDIR的意思是域内指令可以在php.ini、httpd.conf或.htaccess文件中修改。PHP_INI_SYSTEM 域内指令可以在php.ini和httpd.conf文件中修改。所以upload_max_filesize用int_set是无法修改的。只有可修改范围是PHP_INI_ALL的才可以用int_set修改。
获取值的方法:
复制代码 代码如下:
if(@ini_get("file_uploads")) {
$arrInfo['fileupload'] = "允许 - 文件 ".ini_get("upload_max_filesize")." - 表单:".ini_get("post_max_size");
}
else {
$arrInfo['fileupload'] = "禁止";
}
if (get_cfg_var('register_globals')){
$arrInfo['onoff'] ="打开";
}else{
$arrInfo['onoff'] = "关闭";
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
