search

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.?文件上传表单

可以如下建立一个特殊的表单来支持文件上传:?

????



????
????
????
????Send?this?file:?
????


????
以上范例中的?__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?数组特性甚至支持文件类型。?



Pictures:







???
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");
????}
}
?>

?

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool