PHP multi-file upload operation,
In the previous article, I talked about the principle of PHP file upload and the simple operation example is single file upload.
http://www.cnblogs.com/lichenwei/p/3879566.html
In fact, multiple file uploads and single file uploads are similar. The principles are the same, but there are some tricks in the code.
The first is the index.html upload form, but the file in the previous file upload form is changed to file[]
<span><!</span><span>DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span>></span> <span><</span><span>html </span><span>xmlns</span><span>="http://www.w3.org/1999/xhtml"</span><span> xml:lang</span><span>="en"</span><span>></span> <span><</span><span>head</span><span>></span> <span><</span><span>meta </span><span>http-equiv</span><span>="Content-Type"</span><span> content</span><span>="text/html;charset=UTF-8"</span><span>></span> <span><</span><span>title</span><span>></span>upload files<span></</span><span>title</span><span>></span> <span></</span><span>head</span><span>></span> <span><</span><span>body</span><span>></span> <span><</span><span>form </span><span>action</span><span>="upload.php"</span><span> enctype</span><span>="multipart/form-data"</span><span> method</span><span>="post"</span><span>></span> <span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="MAX_FILE_SIZE"</span><span> value</span><span>="100000"</span> <span>/></span><span> 上传文件:</span><span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="file[]"</span><span>/><</span><span>br</span><span>/></span><span> 上传文件:</span><span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="file[]"</span><span>/><</span><span>br</span><span>/></span><span> 上传文件:</span><span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="file[]"</span><span>/><</span><span>br</span><span>/></span> <span><</span><span>input </span><span>type</span><span>="submit"</span><span> value</span><span>="上传"</span> <span>/></span> <span></</span><span>form</span><span>></span> <span></</span><span>body</span><span>></span> <span></</span><span>html</span><span>></span>
Use $_FILES in upload.php to print it
<?<span>php <br /></span><span> print_r</span>(<span>$_FILES</span>); <br />?>
Get the following multi-dimensional array
<span>Array</span><span> ( [</span><span>file</span>] => <span>Array</span><span> ( [name] </span>=> <span>Array</span><span> ( [</span>0] => 照片1.<span>jpg [</span>1] => 照片2.<span>jpg [</span>2] => 照片3.<span>jpg ) [type] </span>=> <span>Array</span><span> ( [</span>0] => image/<span>jpeg [</span>1] => image/<span>jpeg [</span>2] => image/<span>jpeg ) [tmp_name] </span>=> <span>Array</span><span> ( [</span>0] => F:\wamp\tmp\php36C7.<span>tmp [</span>1] => F:\wamp\tmp\php36C8.<span>tmp [</span>2] => F:\wamp\tmp\php36C9.<span>tmp ) [error] </span>=> <span>Array</span><span> ( [</span>0] => 0<span> [</span>1] => 0<span> [</span>2] => 0<span> ) [size] </span>=> <span>Array</span><span> ( [</span>0] => 0<span> [</span>1] => 0<span> [</span>2] => 0<span> ) ) )</span>
According to the principle of single file upload, first think about what we need to get?
Obviously we need to get an array of file information. The array contains name, type, tmp_name, error, size. What we get at this time is a multi-dimensional array. Although the corresponding key values exist, it is multi-dimensional. ,
We only need to split it, such as the three files above, we only need to split it into the corresponding three file information arrays.
Structure of split array
<span>Array</span><span> ( [</span>0] => <span>Array</span><span> ( [name] </span>=> 照片1.<span>jpg [type] </span>=> image/<span>jpeg [tmp_name] </span>=> F:\wamp\tmp\php13C1.<span>tmp [error] </span>=> 0<span> [size] </span>=> 385150<span> ) [</span>1] => <span>Array</span><span> ( [name] </span>=> 照片2.<span>jpg [type] </span>=> image/<span>jpeg [tmp_name] </span>=> F:\wamp\tmp\php13D2.<span>tmp [error] </span>=> 0<span> [size] </span>=> 242043<span> ) [</span>2] => <span>Array</span><span> ( [name] </span>=> 照片3.<span>jpg [type] </span>=> image/<span>jpeg [tmp_name] </span>=> F:\wamp\tmp\php13D3.<span>tmp [error] </span>=> 0<span> [size] </span>=> 488293<span> ) )</span>
The following is the code for splitting and reorganizing the array
<?<span>php </span><span>//</span><span>print_r($_FILES['file']);</span> <span>$arr</span>=<span>$_FILES</span>['file'<span>]; </span><span>$files</span>=<span>array</span><span>(); </span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>count</span>(<span>$arr</span>['name']);<span>$i</span>++<span>){ </span><span>$files</span>[<span>$i</span>]['name']=<span>$arr</span>['name'][<span>$i</span><span>]; </span><span>$files</span>[<span>$i</span>]['type']=<span>$arr</span>['type'][<span>$i</span><span>]; </span><span>$files</span>[<span>$i</span>]['tmp_name']=<span>$arr</span>['tmp_name'][<span>$i</span><span>]; </span><span>$files</span>[<span>$i</span>]['error']=<span>$arr</span>['error'][<span>$i</span><span>]; </span><span>$files</span>[<span>$i</span>]['size']=<span>$arr</span>['size'][<span>$i</span><span>]; } </span><span>print_r</span>(<span>$files</span>);<br />?>
The rest is simple. Just repeat the steps of uploading a single file and iterate through the array.
The code is as follows:
<?<span>php </span><span>//</span><span>print_r($_FILES['file']);</span> <span>$arr</span>=<span>$_FILES</span>['file'<span>]; </span><span>$files</span>=<span>array</span><span>(); </span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>count</span>(<span>$arr</span>['name']);<span>$i</span>++){<span>//</span><span>count()统计数组键值name长度</span> <span>$files</span>[<span>$i</span>]['name']=<span>$arr</span>['name'][<span>$i</span><span>]; </span><span>$files</span>[<span>$i</span>]['type']=<span>$arr</span>['type'][<span>$i</span><span>]; </span><span>$files</span>[<span>$i</span>]['tmp_name']=<span>$arr</span>['tmp_name'][<span>$i</span><span>]; </span><span>$files</span>[<span>$i</span>]['error']=<span>$arr</span>['error'][<span>$i</span><span>]; </span><span>$files</span>[<span>$i</span>]['size']=<span>$arr</span>['size'][<span>$i</span><span>]; } </span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>count</span>(<span>$files</span>);<span>$i</span>++<span>){ </span><span>//</span><span>取得上传文件信息</span> <span>$fileName</span>=<span>$files</span>[<span>$i</span>]['name'<span>]; </span><span>$fileType</span>=<span>$files</span>[<span>$i</span>]['type'<span>]; </span><span>$fileError</span>=<span>$files</span>[<span>$i</span>]['type'<span>]; </span><span>$fileSize</span>=<span>$files</span>[<span>$i</span>]['size'<span>]; </span><span>$tempName</span>=<span>$files</span>[<span>$i</span>]['tmp_name'];<span>//</span><span>临时文件名 //定义上传文件类型</span> <span>$typeList</span> = <span>array</span>("image/jpeg","image/jpg","image/png","image/gif"); <span>//</span><span>定义允许的类型</span> <span>if</span>(<span>$fileError</span>>0<span>){ </span><span>//</span><span>上传文件错误编号判断</span> <span>switch</span> (<span>$fileError</span><span>) { </span><span>case</span> 1: <span>$message</span>="上传的文件超过了php.ini 中 upload_max_filesize 选项限制的值。"<span>; </span><span>break</span><span>; </span><span>case</span> 2: <span>$message</span>="上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。"<span>; </span><span>break</span><span>; </span><span>case</span> 3: <span>$message</span>="文件只有部分被上传。"<span>; </span><span>break</span><span>; </span><span>case</span> 4: <span>$message</span>="没有文件被上传。"<span>; </span><span>break</span><span>; </span><span>case</span> 6: <span>$message</span>="找不到临时文件夹。"<span>; </span><span>break</span><span>; </span><span>case</span> 7: <span>$message</span>="文件写入失败"<span>; </span><span>break</span><span>; </span><span>case</span> 8: <span>$message</span>="由于PHP的扩展程序中断了文件上传"<span>; </span><span>break</span><span>; } </span><span>exit</span>("文件上传失败:".<span>$message</span><span>); } </span><span>if</span>(!<span>is_uploaded_file</span>(<span>$tempName</span><span>)){ </span><span>//</span><span>判断是否是POST上传过来的文件</span> <span>exit</span>("不是通过HTTP POST方式上传上来的"<span>); }</span><span>else</span><span>{ </span><span>if</span>(!<span>in_array</span>(<span>$fileType</span>, <span>$typeList</span><span>)){ </span><span>exit</span>("上传的文件不是指定类型"<span>); }</span><span>else</span><span>{ </span><span>if</span>(!<span>getimagesize</span>(<span>$tempName</span><span>)){ </span><span>//</span><span>避免用户上传恶意文件,如把病毒文件扩展名改为图片格式</span> <span>exit</span>("上传的文件不是图片"<span>); } } </span><span>if</span>(<span>$fileSize</span>>1000000<span>){ </span><span>//</span><span>对特定表单的上传文件限制大小</span> <span>exit</span>("上传文件超出限制大小"<span>); }</span><span>else</span><span>{ </span><span>//</span><span>避免上传文件的中文名乱码</span> <span>$fileName</span>=<span>iconv</span>("UTF-8", "GBK", <span>$fileName</span>);<span>//</span><span>把iconv抓取到的字符编码从utf-8转为gbk输出</span> <span>$fileName</span>=<span>str_replace</span>(".", <span>time</span>().".", <span>$fileName</span>);<span>//</span><span>在图片名称后加入时间戳,避免重名文件覆盖</span> <span>if</span>(<span>move_uploaded_file</span>(<span>$tempName</span>, "uploads/".<span>$fileName</span><span>)){ </span><span>echo</span> "上传文件成功!"<span>; }</span><span>else</span><span>{ </span><span>echo</span> "上传文件失败"<span>; } } } } </span>?>
The effect is as follows:

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
