Home php教程 php手册 配置php.ini实现PHP文件上传功能

配置php.ini实现PHP文件上传功能

Jun 06, 2016 pm 08:16 PM
php.ini php file upload Configuration

为大家介绍下在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 of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set up Git configuration in PyCharm How to set up Git configuration in PyCharm Feb 20, 2024 am 09:47 AM

Title: How to correctly configure Git in PyCharm In modern software development, the version control system is a very important tool, and Git, as one of the popular version control systems, provides developers with powerful functions and flexible operations. As a powerful Python integrated development environment, PyCharm comes with support for Git, allowing developers to manage code versions more conveniently. This article will introduce how to correctly configure Git in PyCharm to facilitate better development during the development process.

The working principle and configuration method of GDM in Linux system The working principle and configuration method of GDM in Linux system Mar 01, 2024 pm 06:36 PM

Title: The working principle and configuration method of GDM in Linux systems In Linux operating systems, GDM (GNOMEDisplayManager) is a common display manager used to control graphical user interface (GUI) login and user session management. This article will introduce the working principle and configuration method of GDM, as well as provide specific code examples. 1. Working principle of GDM GDM is the display manager in the GNOME desktop environment. It is responsible for starting the X server and providing the login interface. The user enters

The perfect combination of PyCharm and PyTorch: detailed installation and configuration steps The perfect combination of PyCharm and PyTorch: detailed installation and configuration steps Feb 21, 2024 pm 12:00 PM

PyCharm is a powerful integrated development environment (IDE), and PyTorch is a popular open source framework in the field of deep learning. In the field of machine learning and deep learning, using PyCharm and PyTorch for development can greatly improve development efficiency and code quality. This article will introduce in detail how to install and configure PyTorch in PyCharm, and attach specific code examples to help readers better utilize the powerful functions of these two. Step 1: Install PyCharm and Python

Understand Linux Bashrc: functions, configuration and usage Understand Linux Bashrc: functions, configuration and usage Mar 20, 2024 pm 03:30 PM

Understanding Linux Bashrc: Function, Configuration and Usage In Linux systems, Bashrc (BourneAgainShellruncommands) is a very important configuration file, which contains various commands and settings that are automatically run when the system starts. The Bashrc file is usually located in the user's home directory and is a hidden file. Its function is to customize the Bashshell environment for the user. 1. Bashrc function setting environment

Avoid common mistakes in Maven environment configuration: Solve configuration problems Avoid common mistakes in Maven environment configuration: Solve configuration problems Feb 19, 2024 pm 04:56 PM

Maven is a Java project management and build tool that is widely used in the development of Java projects. In the process of using Maven to build projects, you often encounter some common environment configuration problems. This article will answer these common questions and provide specific code examples to help readers avoid common configuration errors. 1. Maven environment variables are incorrectly configured. Problem description: When using Maven, if the environment variables are incorrectly configured, Maven may not work properly. Solution: Make sure

Simple and easy-to-understand PyCharm configuration Git tutorial Simple and easy-to-understand PyCharm configuration Git tutorial Feb 20, 2024 am 08:28 AM

PyCharm is a commonly used integrated development environment (IDE). In daily development, using Git to manage code is essential. This article will introduce how to configure Git in PyCharm and use Git for code management, with specific code examples. Step 1: Install Git First, make sure Git is installed on your computer. If it is not installed, you can go to [Git official website](https://git-scm.com/) to download and install the latest version of Git

MyBatis Generator configuration parameter interpretation and best practices MyBatis Generator configuration parameter interpretation and best practices Feb 23, 2024 am 09:51 AM

MyBatisGenerator is a code generation tool officially provided by MyBatis, which can help developers quickly generate JavaBeans, Mapper interfaces and XML mapping files that conform to the database table structure. In the process of using MyBatisGenerator for code generation, the setting of configuration parameters is crucial. This article will start from the perspective of configuration parameters and deeply explore the functions of MyBatisGenerator.

How to configure workgroup in win11 system How to configure workgroup in win11 system Feb 22, 2024 pm 09:50 PM

How to configure a workgroup in Win11 A workgroup is a way to connect multiple computers in a local area network, which allows files, printers, and other resources to be shared between computers. In Win11 system, configuring a workgroup is very simple, just follow the steps below. Step 1: Open the "Settings" application. First, click the "Start" button of the Win11 system, and then select the "Settings" application in the pop-up menu. You can also use the shortcut "Win+I" to open "Settings". Step 2: Select "System" In the Settings app, you will see multiple options. Please click the "System" option to enter the system settings page. Step 3: Select "About" In the "System" settings page, you will see multiple sub-options. Please click

See all articles