php文件下传介绍
php文件上传介绍
?
PHP手册第39章有关于处理这些问题的说明,我复制粘贴如下:
--------------------------------------------
第?39?章?文件上传处理
目录
POST?方法上传
错误信息说明
常见缺陷
上传多个文件
对?PUT?方法的支持
POST?方法上传
本特性可以使用户上传文本和二进制文件。用?PHP?的认证和文件操作函数,可以完全控制允许哪些人上传以及文件上传后怎样处理。?
PHP?能够接受任何来自符合?RFC-1867?标准的浏览器(包括?Netscape?Navigator?3?及更高版本,打了补丁的?Microsoft?Internet?Explorer?3?或者更高版本)上传的文件。?
相关的设置:?请参阅?php.ini?的?file_uploads,upload_max_filesize,upload_tmp_dirpost_max_size?以及?max_input_time?设置选项。?
请注意?PHP?也支持?PUT?方法的文件上传,Netscape?Composer?和?W3C?的?Amaya?客户端使用这种方法。请参阅对?PUT?方法的支持以获取更多信息。?
例?39.1.?文件上传表单
可以如下建立一个特殊的表单来支持文件上传:?
????
????
以上范例中的?__URL__?应该被换掉,指向一个真实的?PHP?文件。?
MAX_FILE_SIZE?隐
藏字段(单位为字节)必须放在文件输入字段之前,其值为接收文件的最大尺寸。这是对浏览器的一个建议,PHP?也会检查此项。在浏览器端可以简单绕过此设
置,因此不要指望用此特性来阻挡大文件。实际上,PHP?设置中的上传文件最大值是不会失效的。但是最好还是在表单中加上此项目,因为它可以避免用户在花
时间等待上传大文件之后才发现文件过大上传失败的麻烦。?
注意:?要确保文件上传表单的属性是?enctype="multipart/form-data",否则文件上传不了。?
全局变量?$_FILES?自?PHP?4.1.0?起存在(在更早的版本中用?$HTTP_POST_FILES?替代)。此数组包含有所有上传的文件信息。?
以上范例中?$_FILES?数组的内容如下所示。我们假设文件上传字段的名称如上例所示,为?userfile。名称可随意命名。?
$_FILES['userfile']['name']
客户端机器文件的原名称。?
$_FILES['userfile']['type']
文件的?MIME?类型,如果浏览器提供此信息的话。一个例子是“image/gif”。不过此?MIME?类型在?PHP?端并不检查,因此不要想当然认为有这个值。?
$_FILES['userfile']['size']
已上传文件的大小,单位为字节。?
$_FILES['userfile']['tmp_name']
文件被上传后在服务端储存的临时文件名。?
$_FILES['userfile']['error']
和该文件上传相关的错误代码。此项目是在?PHP?4.2.0?版本中增加的。?
文
件被上传后,默认地会被储存到服务端的默认临时目录中,除非?php.ini?中的?upload_tmp_dir?设置为其它的路径。服务端的默认临时
目录可以通过更改?PHP?运行环境的环境变量?TMPDIR?来重新设置,但是在?PHP?脚本内部通过运行?putenv()?函数来设置是不起作用
的。该环境变量也可以用来确认其它的操作也是在上传的文件上进行的。?
例?39.2.?使文件上传生效
请查阅函数?is_uploaded_file()?和?move_uploaded_file()?以获取进一步的信息。以下范例处理由表单提供的文件上传。?
//?In?PHP?versions?earlier?than?4.1.0,?$HTTP_POST_FILES?should?be?used?instead
//?of?$_FILES.
$uploaddir?=?'/var/www/uploads/';
$uploadfile?=?$uploaddir?.?basename($_FILES['userfile']['name']);
echo?'';<br>
if?(move_uploaded_file($_FILES['userfile']['tmp_name'],?$uploadfile))?{<br>
????echo?"File?is?valid,?and?was?successfully?uploaded.\n";<br>
}?else?{<br>
????echo?"Possible?file?upload?attack!\n";<br>
}<br><br>
echo?'Here?is?some?more?debugging?info:';<br>
print_r($_FILES);<br><br>
print?"
?>?
接
受上传文件的?PHP?脚本为了决定接下来要对该文件进行哪些操作,应该实现任何逻辑上必要的检查。例如可以用?$_FILES['userfile']
['size']?变量来排除过大或过小的文件,也可以通过?$_FILES['userfile']['type']?变量来排除文件类型和某种标准不
相符合的文件,但只把这个当作一系列检查中的第一步,因为此值完全由客户端控制而在?PHP?端并不检查。自?PHP?4.2.0?起,还可以通
过?$_FILES['userfile']['error']?变量来根据不同的错误代码来计划下一步如何处理。不管怎样,要么将该文件从临时目录中删
除,要么将其移动到其它的地方。?
如果表单中没有选择上传的文件,则?PHP?变量?$_FILES['userfile']['size']?的值将为?0,$_FILES['userfile']['tmp_name']?将为空。?
如果该文件没有被移动到其它地方也没有被改名,则该文件将在表单请求结束时被删除。?
例?39.3.?上传一组文件
PHP?的?HTML?数组特性甚至支持文件类型。?
???
foreach?($_FILES["pictures"]["error"]?as?$key?=>?$error)?{
????if?($error?==?UPLOAD_ERR_OK)?{
????????$tmp_name?=?$_FILES["pictures"]["tmp_name"][$key];
????????$name?=?$_FILES["pictures"]["name"][$key];
????????move_uploaded_file($tmp_name,?"data/$name");
????}
}
?>
?

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.
