search
HomeBackend DevelopmentPHP Tutorial配置php.ini实现PHP文件上传功能_php技巧

昨天分享了在PHP网站开发中如何在php.ini中配置实现session功能的PHP教程,今天继续分享在利用PHP实现文件上传功能时几点关键php.ini的配置。

  说到在php.ini中的文件上传的配置,其实在之前介绍PHP文件上传功能代码实例教程以及Jquery AjaxUpload实现文件上传功能代码实例教程时我都有所提及。PHP文件上传功能配置主要涉及php.ini配置文件中的upload_tmp_dir、upload_max_filesize、post_max_size等选项。

  php.ini中文件上传功能配置选项说明

  打开php.ini配置文件找到File Uploads

  file_uploads = On

  默认允许HTTP文件上传,此选项不能设置为OFF。

  upload_tmp_dir =

  默认为空,此选项在手动配置PHP运行环境时,也容易遗忘,如果不配置这个选项,文件上传功能就无法实现,这个选项设置的是文件上传时存放文件的临时目录,你必须给这个选项赋值,比如upload_tmp_dir ='/leapsoulcn',代表在C盘目录下有一个leapsoulcn目录,和session配置一样,如果你是在linux环境下,你必须赋予这个目录可写权限。

  如何上传超过8M的大文件?

  上传大文件主要涉及配置upload_max_filesize和post_max_size两个选项。

  php.ini配置文件中的默认文件上传大小为2M,php初学者容易犯的一个错误是在编写文件上传功能时通过设置上传文件最大大小的表单区域,即允许上传文件的最大值,max_file_size(隐藏值域)的值来规定上传文件的大小,其实一般别人可以绕过这个值,所以安全起见,最好是在php.ini配置文件中配置upload_max_filesize选项,设定文件上传的大小。

  默认upload_max_filesize = 2M,即文件上传的大小为2M,如果你想上传超过8M的文件,比如20M,你必须设定upload_max_filesize = 20M。

  但是光设置upload_max_filesize = 20M还是无法实现大文件的上传功能,你必须修改php.ini配置文件中的post_max_size选项,其代表允许POST的数据最大字节长度,默认为8M。如果POST数据超出限制,那么$_POST和$_FILES将会为空。要上传大文件,你必须设定该选项值大于upload_max_filesize指令的值,我一般设定upload_max_filesize和post_max_size值相等。另外如果启用了内存限制,那么该值应当小于memory_limit选项的值。

  文件上传的其他注意事项

  在上传大文件时,你会有上传速度慢的感觉,当超过一定的时间,会报脚本执行超过30秒的错误,这是因为在php.ini配置文件中max_execution_time配置选项在作怪,其表示每个脚本最大允许执行时间(秒),0 表示没有限制。你可以适当调整max_execution_time的值,不推荐设定为0。

  至此,在php.ini配置文件中对文件上传选项进行配置的PHP教程就介绍完毕了,通过上面的步骤实践与学习,再结合PHP程序,文件上传功能就可以实现了。

下面是一些补充:


本文介绍了如何配置php.ini实现PHP文件上传功能。其中涉及到php.ini配置文件中的upload_tmp_dir、upload_max_filesize、post_max_size等选项,这些选项是文件上传成败的关键。我们以php.5.3.5的Windows版本为例说明。

php.ini中文件上传功能配置选项说明
用文本工具(推荐EditPlus)打开php.ini 配置文件,查找 File Uploads ,在这个区域有以下3个选项:

file_uploads = On

是否允许HTTP文件上传。默认值为On允许HTTP文件上传,此选项不能设置为Off。

upload_tmp_dir =

文件上传的临时存放目录。如果没指定则PHP会使用系统默认的临时目录。该选项默认为空,此选项在手动配置PHP运行环境时,也容易遗忘,如果不配置这个选项,文件上传功能就无法实现,你必须给这个选项赋值,比如upload_tmp_dir = "d:/fileuploadtmp" ,代表在D盘目录下有一个fileuploadtmp目录,并且给这目录读写权限。

upload_max_filesize = 2M

上传文件的最大尺寸。这个选项默认值为2M,即文件上传的大小为2M,如果你想上传一个50M的文件,你必须设定 upload_max_filesize = 50M。
但是仅设置upload_max_filesize = 50M 还是无法实现大文件的上传功能,我们还必须修改php.ini文件中的 post_max_size 选项。

继续在 php.ini 中查找 Data Handling ,在这个区域有1个选项:

post_max_size = 8M

指通过表单POST给PHP的所能接收的最大值,包括表单里的所有值。默认为8M。如果POST数据超出限制,那么$_POST和$_FILES将会为空。
要上传大文件,你必须设定该选项值大于upload_max_filesize选项的值,例如你设置了upload_max_filesize = 50M ,这里可以把post_max_size = 100M。
另外如果启用了内存限制,那么该值应当小于memory_limit 选项的值。

继续在 php.ini 中查找 Resource Limits ,在这个区域有3个选项:

max_execution_time = 30

每个PHP页面运行的最大时间值(单位秒),默认30秒。当我们上传一个较大的文件,例如50M的文件,很可能要几分钟才能上传完,但php默认页面最久执行时间为30秒,超过30秒,该脚本就停止执行,这就导致出现无法打开网页的情况。因此我们可以把值设置的较大些,如 max_execution_time = 600。 如果设置为0,则表示无时间限制。

max_input_time = 60

每个PHP脚本解析请求数据所用的时间(单位秒),默认60秒。当我们上传大文件时,可以将这个值设置的较大些。 如果设置为0,则表示无时间限制。

memory_limit = 128M

这个选项用来设置单个PHP脚本所能申请到的最大内存空间。这有助于防止写得不好的脚本消耗光服务器上的可用内存。如果不需要任何内存上的限制将其设为 -1。
php5.2.0以前的版本默认8M; php.5.2.0版本默认为16M。php 5.2.0之后的版本默认为 128M;

php.ini 配置上传文件功能示例
假设要上传一个50M的大文件。配置 php.ini 如下:
file_uploads = On
upload_tmp_dir = "d:/fileuploadtmp"
upload_max_filesize = 50M
post_max_size = 100M
max_execution_time = 600
max_input_time = 600
memory_limit = 128M

提示:需要保持 memory_limit > post_max_size > upload_max_filesize

此例仅供参考,你可以根据实际情况调整。
 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.