5步详解PHP文件下传功能的实现
5步详解PHP文件上传功能的实现
1、表单部分
允许用户上传文件,在HTML表单的声明中要加上一个上传的属性:
enctype = 'multipart/form-data'
表单的method必须是POST
表单选项MAX_FILE_SIZE隐藏域用于限制上传文件大小,它必须放在文件表单元素前面,单位为字节。
如:
2、处理上传文件
上传时,PHP收到关于该文件的信息数组,这些信息可以在$_FILES这个超级全局数组中找到。
如:如果表单中的文件输入框名字为upload_file,那么关于该文件的所有信息都包含在数组$_FILES['upload_file']里面。
如:客户上传了一个“aaa.jpg”的图片数组值如下:
name "p5pp.jpg" 上传时文件的名字
type “image/jpeg" 文件类型
tmp_name "/tmp/phpjksdf" 服务器端的临时文件名
error 上传错误的返回值
size 2045 文件实际大小
上面数组里面的error会返回不同的常量值,如下:
UPLOAD_ERR_OK 没有错误发生,文件上传成功
UPLOAD_ERR_INI_SIZE 文件大小超过了PHP.INI中upload_max_filesize选项限制的值
UPLOAD_ERR_FORM_SIZE 上传文件的大小超过了HTML表单中MAX_FILE_SIZE选项值。可在程序中检查表单$FILES ['up_file']['size']来处理
UPLOAD_ERR_PARTIAL 文件只有部分被上传
UPLOAD_ERR_NO_FILE 用户没有提供任何文件上传
上传后处理的具体例子:
if(!move_uploaded_file($_FILES['f']['tmp_name'],"uploads/".$_FILES['f']['name'].".jpg")){
echo "error";
}
函数:
move_uploaded_file 移动上传的临时文件到指定的目录下
例子:
move_uploaded_file('临时文件名','指定文件路径')
is_uploaded_file 判断是否是通过http Post上传的文件
例子:
if(!is_uploaded_file($_FILES['f']['tmp_name'])){
echo '非法';
}
3、相关参数
PHP上传设计到的php.ini中的参数:
file_uploads 是否允许上传文件,默认ON
upload_tmp_dir 上传文件防止的临时目录,未指定则使用系统默认位置
upload_max_filesize 允许上传文件的大小的最大值,默认为2M
post_max_size 控制采用POST方法进行一次表单提交中PHP所能接受的最大数据量,如果希望用PHP文件上传,则此值要改为比upload_max_filesize 要大
max_input_time 以秒为单位对通过POST/GET/PUT方式接受数据时间进行限制。
memory_limit 为了避免正在运行的脚本大量使用系统内存,PHP允许定义内存使用限额。通过设置此参数来制定单个脚本程序可以使用的最大内存容量,应适当大于 post_max_size值
max_execution_time 用来设置在强制终止脚本前PHP等待脚本执行完毕的时间,单位秒。次选项可限制死循环脚本,但当存在一个长时间的合法活动时(如:上传大文件),这项功能也会导致操作失败。这样情况下必须考虑将此变量增加。
4、考虑多文件上传
可以利用$_FILES数组就可以轻松实现多文件上传。$_FILES数组可以获取客户端表单里面所有的file域内容,从而获得所有在同一表单上传的文件。
5、突破上传的内存限制
方法一:
修改php.ini中memory_limit值,改为更大,如 64M
方法二:
使用Apache Rewrite方法,动态修改memory_limit的值。首先建立一个.htaccess文件,保存在上传文件程序的 当前目录即可.代码如下:
php_value memory_limit 100M
php_value post_max_size 30M
php_value upload_max_filesize 30M
php_value max_execution_time 300
php_value max_input_tim 300
php_value display_errors On
——EOF——
看啦上面的文章,希望对你有所收获。
摘自红色黑客联盟(www.7747.net) 原文:http://www.7747.net/kf/201009/73557.html

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

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

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











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

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.
