PHP文件上传流程
1、单击提交按钮,浏览器用户将包含上传文件的表单数据提交给PHP处理程序
2、 Web服务器和PHP预处理器首先判断表单数据的大小是否超过php.ini配置文件中的post_max_size选项设置的上限值。
若超过,PHP处理程序将无法得到任何表单数据,此时不仅上传文件失败,而且表单控件中填写的数据也会提交失败,也就是说:PHP处理程序预定义变量$_GET、$_POST、$_FILES将为空数组。若没有超过,文件上传进入第3步检验。
3. 检验表单中的文件大小是否超过表单隐藏域MAX_FILE_SIZE设置的上限值。
若超过,PHP预处理器返回状态码2,文件上传失败。
若没有超过,文件上传进入第4步检验。
(当有多个文件进行上传时,某个文件上传框导致的文件上传失败,不会影响其他文件上传框的上传结果)
4. 检验表单中的文件是否超过php.ini配置文件中upload_max_filesize选项设置的上限值。
若超过,PHP预处理器返回状态码1,文件上传失败。
若没有超过,文件上传进入第5步检验。
5. PHP实现上传文件需要在php.ini配置文件upload_tmp_dir选项定义的目录中创建一个与上传文件一一对应的临时文件(默认拓展名为tmp),上传成功后,临时文件立即消失,此时PHP预处理器的返回状态码0。
但是有时由于默写原因(如max_execution_time选项设置过小或者网速慢等原因),上传部分文件后不再继续上传剩余文件,导致文件上传失败,此时PHP预处理器返回状态码3
若通过,文件上传进入第6步检验。
6. 实现文件上传的关键一步在于在临时文件消失前,需要将临时文件保存到Web服务器或文件服务器。PHP提供的两个函数:is_uploaded_file()函数和move_uploaded_file()函数,可以帮助完成这一步的工作
多个文件上传要注意的就是相同的name保存的文件内容是按照下面的形式放在数组中的。是五个数组,按照文件的五个参数分别存放的,并非三个数组。所以如果直接使用count($_FILES[‘$myPicture’]),答案为5。
array (size=5) 'name' => array (size=3) 0 => string '1.txt' (length=5) 1 => string '2.txt' (length=5) 2 => string '3.txt' (length=5) 'type' => array (size=3) 0 => string 'text/plain' (length=10) 1 => string 'text/plain' (length=10) 2 => string 'text/plain' (length=10) 'tmp_name' => array (size=3) 0 => string 'D:\wamp64\tmp\phpC5E8.tmp' (length=25) 1 => string 'D:\wamp64\tmp\phpC5E9.tmp' (length=25) 2 => string 'D:\wamp64\tmp\phpC5EA.tmp' (length=25) 'error' => array (size=3) 0 => int 0 1 => int 0 2 => int 0 'size' => array (size=3) 0 => int 0 1 => int 0 2 => int 0
index.php文件
<form action="fileSystem.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> <input type="file" name="myPicture[]" size= "25" maxlength="100"><br> <input type="file" name="myPicture[]" size= "25" maxlength="100"><br> <input type="file" name="myPicture[]" size= "25" maxlength="100"><br> <input type="submit" value="提交"> </form>
fileSystem文件
<?php if (empty($_POST)) { exit("提交的表单数据超过post_max_size的配置"); } $arr = $_FILES['myPicture']; $file =array(); for ($i=0; $i < count($arr['name']); $i++) { $file[$i]['name'] = $arr['name'][$i]; $file[$i]['type'] = $arr['type'][$i]; $file[$i]['tmp_name'] = $arr['tmp_name'][$i]; $file[$i]['error'] = $arr['error'][$i]; $file[$i]['size'] = $arr['size'][$i]; } for ($i=0; $i < count($file); $i++) { switch ($file[$i]['error']) { case 0: $fileName = $file[$i]['name']; $fileTemp = $file[$i]['tmp_name']; $destination = "uploads/" . $file[$i]['name']; move_uploaded_file($fileTemp, $destination); echo "上传成功"; break; case 1: echo "上传附件超过php.ini中的upload_max_filesize选项的限制"; break; case 2: echo "上传附件的大小超过了form表单MAX_FILE_SIZE选项指定的值"; break; case 3: echo "附件只有部分被上传"; break; case 4: echo "没有选择上传附件"; break; } } ?>
【相关教程推荐】
1. php编程从入门到精通全套视频教程
2. php从入门到精通
3. bootstrap教程

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor