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

AI Hentai Generator
Generate AI Hentai for free.

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 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
