How to Upload Large Files in PHP

快速阅读提示:请关注红色高亮部分

Uploading a file from a web form in PHP is easy. The online manual provides a Handling File
Uploads
 section, and there are several articles on sitepoint.com, including How
To Handle File Uploads With PHP
 by Kevin Yank.

One of the most popular uses is image uploads. Your users can submit photographs from a form without resorting to FTP or other convoluted methods. HTML5 and Flash also permit drag and drop, so the operation is likely to become easier as browsers evolve.

This is where the problems can begin. Camera manufacturers continually brag that they have a larger set of megapixels than their competitors. It’s all rubbish, of course — unless you’re a professional photographer or need to print extremely large images, anything
over 4MP is fairly pointless and lens quality is much more important. However, even low-end compacts have 12MP and mobile phones have more than 5MP. The result is that a typical snapshot can easily be 6MB in size.

By default, PHP permits a maximum file upload of 2MB. You can ask users to resize their images before uploading but let’s face it: they won’t. Fortunately,we
can increase the limit when necessary.

Two PHP configuration options control the maximum upload size: upload_max_filesize and post_max_size.
Both can be set to, say, “10M” for 10 megabyte file sizes.

However, you also need to consider the time it takes to complete an upload. PHP scripts normally time-out after 30 seconds, but a 10MB file would take at least 3 minutes to upload on a healthy broadband connection (remember that upload speeds are typically
five times slower than download speeds). In addition, manipulating or saving an uploaded image may also cause script time-outs. We therefore need to set PHP’s max_input_timeand max_execution_time to
something like 300 (5 minutes specified in seconds).

These options can be set in your server’s php.ini configuration
file so that they apply to all your applications. Alternatively, if you’re using Apache, you can configure the settings in your application’s .htaccess file:


php_value upload_max_filesize 10M
php_value post_max_size 10M
php_value max_input_time 300
php_value max_execution_time 300

Finally, you can define the constraints within your PHP application:

ini_set('upload_max_filesize', '10M');
ini_set('post_max_size', '10M');
ini_set('max_input_time', 300);
ini_set('max_execution_time', 300);

PHP also provides a set_time_limit() function
so you don’t need to set max_execution_time directly.

Setting the options in your PHP code is possibly more practical, since you can extend the execution time and increase the file size when your application is expecting a large upload. Other forms would revert to the default 30-second time-out and 2MB limit.

Do you have any other tips for uploading large files in PHP?

reference: http://www.sitepoint.com/upload-large-files-in-php/

提示:.htaccess文件应放在根目录中

时间: 2024-08-05 08:39:38

How to Upload Large Files in PHP的相关文章

几个Asp.Net上传文件的控件收集

asp教程net上传文件的几个控件(downmoon收集)   1.aspnetupload 地址:http://www.aspnetupload.net/ 最早接触的上传控件.带进度条,能与系统自带的inputfile控件共用.不过是收费的,而且对firefox支持较差.最新版本是2.3 2.fancyupload - swiff meets ajax (v2.0)(开源) 地址:http://digitarald.de/project/fancyupload/ falsh上传,ajax技术,

AspNet上传文件的几个控件

本文转载:http://www.cnblogs.com/downmoon/archive/2009/02/05/1384931.html 1.AspnetUpload 地址:http://www.aspnetupload.net/ 最早接触的上传控件.带进度条,能与系统自带的InputFile控件共用.不过是收费的,而且对Firefox支持较差.最新版本是2.3 2.FancyUpload - Swiff meets Ajax (v2.0)(开源) 地址:http://digitarald.de

Reading files in JavaScript using the File APIs

Reading files in JavaScript using the File APIs By Eric Bidelman Published: June 18th, 2010Comments: 273 Introduction HTML5 finally provides a standard way to interact with local files, via the File APIspecification. As example of its capabilities, t

PHP开发环境搭建详细教程

     首先你需要安装Apache Server,这里以当前最新版Apache Server2.4为例进行说明,可惜官网并没有提供最新版的msi安装包,连编译后的binary二进制压缩包都没提供,不过我已经编译并上传到我的百度网盘,下载地址如下:       Apache Server2.4最新版安装包   下载下来后直接解压到任意目录,如图:  然后你需要安装Apache Server系统服务,先cd 到apacher server2.4/bin下,然后执行如下命令: Java代码   ht

用两行代码在浏览器中实现文件上传

简介文件上传是将任意的文件从客户机发送到服务器的过程.最简单.最方便的上传方法是使用支持RFC1867的浏览器,如微软的Internet Explorer4.0以上版本,Netscape3.0以上版本,或者带附件的Internet Explorer3.0.基于浏览器的文件上传是通过带有属性ENCTYPE="multipart/form-data"的HTML form实现的.这个form也必须包含一个或多个<INPUT TYPE=FILE>项,以让用户指定要上传的本地文件.

Website Cloud Architecture Best Practices

This article introduces the technical challenges brought by cloud technology to traditional enterprises, and explains in depth the best practices of cloud architecture. It was first published in Lingyun, a magazine jointly sponsored by Alibaba Cloud

Expand Your HTML Page File Cache with Cloud Storage

Background Most people use cloud storage apps, such as Object Storage Service (OSS), for images or other large files. However, cloud storage is not intrinsically limited to a single use case but merely provides a platform for us to solve a wide range

Solve Linux &amp;amp; Unix Systems Hard Disk Problems

8 Tips to Solve Linux & Unix Systems Hard Disk Problems Like Disk Full Or Can't Write to the Disk by NIXCRAFT on OCTOBER 29, 2014 · 5 COMMENTS· LAST UPDATED DECEMBER 9, 2014 in DATACENTER, HARDWARE, STORAGE Can't write to the hard disk on a Linux or

Monit:开源服务器监控工具

Monit是一个跨平台的用来监控Unix/linux系统(比如Linux.BSD.OSX.Solaris)的工具.Monit特别易于安装,而且非常轻量级(只有500KB大小),并且不依赖任何第三方程序.插件或者库. Monit可以监控服务器进程状态.HTTP/TCP状态码.服务器资源变化.文件系统变动等等,根据这些变化,可以设定邮件报警.重启进程或服务.易于安装.轻量级的实现以及强大的功能,让Monit成为一个理想的后备监控工具. 官网:https://mmonit.com/monit 文档:h