When learning PHP, you may encounter the problem of uploading multiple pictures in PHP. Here we will introduce the solution to the problem of uploading multiple pictures in PHP and share it with you here. Uploading a single image is not complicated. It involves PHP uploading multiple images and verifying the image format to ensure that the uploaded image must be an image and prevent other files from being uploaded to the server.
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>//图片目录 </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>img_dir</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"../upload/"</SPAN><SPAN>; </SPAN></SPAN><LI class=alt><SPAN>//……html显示上传界面 </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>/*图片上传处理*/ </SPAN><LI class=""><SPAN>//把图片传到服务器 </SPAN><LI class=alt><SPAN>//初始化变量 </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>uploaded</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>0</SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>unuploaded</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>0</SPAN><SPAN>; </SPAN></SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>//只允许五张图片上传 </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>for($</SPAN><SPAN class=attribute>i</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>0</SPAN><SPAN>;$i</SPAN><SPAN class=tag><</SPAN><SPAN>=5;$i++) </SPAN></SPAN><LI class=alt><SPAN>{ </SPAN><LI class=""><SPAN>//获取当前图片的信息 </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>is_file</SPAN><SPAN>=$_FILES['imgfile']['name'][$i]; </SPAN></SPAN><LI class=""><SPAN>//如果当前图片不为空 </SPAN><LI class=alt><SPAN>if(!empty($is_file)) </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>//把当前图片的信息存储到变量里 </SPAN><LI class=""><SPAN>$result[$i]=" </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><</SPAN><SPAN class=attribute>trclass</SPAN><SPAN class=tag-name>trclass</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>td2align</SPAN><SPAN>=center</SPAN><SPAN class=tag>></span><span> </span></span></li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span>".$_FILES['imgfile']['name'][$i]."</span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span>".round($_FILES['imgfile']['size'][$i]/1024,2)."K</span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span>".$_FILES['imgfile']['type'][$i]."</span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span>"; </span> </li> <li class=""><span> </span></li> <li class="alt"><span>//判断上传的图片的类型是不是jpg,gif,png,bmp中的一种,同时判断是否上传成功 </span></li> <li class=""><span>if( </span></li> <li class="alt"><span> </span></li> <li class=""><span>$_FILES['imgfile']['type'][$i]=="image/pjpeg"|| </span></li> <li class="alt"><span>$_FILES['imgfile']['type'][$i]=="image/gif"|| </span></li> <li class=""><span>$_FILES['imgfile']['type'][$i]=="image/x-png"|| </span></li> <li class="alt"><span>$_FILES['imgfile']['type'][$i]=="image/bmp" </span></li> <li class=""><span>) </span></li> <li class="alt"><span>{ </span></li> <li class=""><span>//如果上传的文件没有在服务器上存在 </span></li> <li class="alt"><span>if(!file_exists($img_dir.$_FILES['imgfile']['name'][$i])) </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>//把图片文件从临时文件夹中转移到我们指定上传的目录中 </span></li> <li class=""><span>move_uploaded_file($_FILES['imgfile']['tmp_name'][$i], </span></li> <li class="alt"><span>$img_dir.$_FILES['imgfile']['name'][$i]); </span></li> <li class=""> <span>$result[$i]</span><span class="attribute">.</span><span>=</span><span class="attribute-value">"成功"</span><span>; </span> </li> <li class="alt"><span>$uploaded++; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>else//如果文件已经在服务器上存在 </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>$result[$i]</span><span class="attribute">.</span><span>=</span><span class="attribute-value">"<fontcolor=red>文件已存在</font>"</span><span>; </span> </li> <li class=""><span>$unuploaded++; </span></li> <li class="alt"><span>continue; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""><span>else </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>$result[$i]</span><span class="attribute">.</span><span>=</span><span class="attribute-value">"<fontcolor=red>失败</font>"</span><span>; </span> </li> <li class="alt"><span>$unuploaded++; </span></li> <li class=""><span>} </span></li> <li class="alt"> <span>$result[$i]</span><span class="attribute">.</span><span>=</span><span class="attribute-value">"</td></tr>"</span><span>; </span> </li> <li class=""><span>}//endif </span></li> <li class="alt"><span>}//endfor </span></li> <li class=""><span> </span></li> <li class="alt"><span> </span></li> <li class=""><span>//如果没有选择任何图片 </span></li> <li class="alt"><span>if(empty($result)) </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>prompt_msg("错误信息","没有选择任何图片。","返回上一步","uploadimg.php?</span><span class="attribute">action</span><span>=</span><span class="attribute-value">upload</span><span>"); </span> </li> <li class=""><span>exit(); </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>//显示所有上传后的结果 </span></li> <li class=""> <span>echo"</span><span class="tag"><</SPAN><SPAN class=attribute>tablecellpadding</SPAN><SPAN class=tag-name>tablecellpadding</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>4cellspacing</SPAN><SPAN>=</SPAN><SPAN class=attribute>1border</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>0class</SPAN><SPAN>=</SPAN><SPAN class=attribute>tablewidth</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>400align</SPAN><SPAN>=left</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=attribute>trclass</SPAN><SPAN class=tag-name>trclass</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>navialign</SPAN><SPAN>=center</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span>文件名</span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span>大小</span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span>类型</span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span>上传结果</span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"></</SPAN><SPAN class=tag-name>tr</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"><span>"; </span></li> <li class=""><span> </span></li> <li class="alt"><span>foreach($resultas$value) </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>echo$value; </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""> <span>echo"</span><span class="tag"><</SPAN><SPAN class=attribute>trclass</SPAN><SPAN class=tag-name>trclass</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>td1</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=attribute>tdcolspan</SPAN><SPAN class=tag-name>tdcolspan</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>4</SPAN><SPAN class=tag>></span><span>共上传".($uploaded+$unuploaded).",成功:$uploaded,失败:</span><span class="tag"><</SPAN><SPAN class=attribute>fontcolor</SPAN><SPAN class=tag-name>fontcolor</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>red</SPAN><SPAN class=tag>></span><span>$unuploaded</span><span class="tag"></</SPAN><SPAN class=tag-name>font</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"></</SPAN><SPAN class=tag-name>tr</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=attribute>trclass</SPAN><SPAN class=tag-name>trclass</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>navi</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=attribute>tdcolspan</SPAN><SPAN class=tag-name>tdcolspan</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>4align</SPAN><SPAN>=center</SPAN><SPAN class=tag>></span><span>[</span><span class="tag"><</SPAN><SPAN class=attribute>ahref</SPAN><SPAN class=tag-name>ahref</SPAN><SPAN>='uploadimg.php?action=upload'</SPAN><SPAN class=attribute-value>title</SPAN><SPAN>='继续上传'</SPAN><SPAN class=tag>></span><span>继续上传</span><span class="tag"></</SPAN><SPAN class=tag-name>a</SPAN><SPAN class=tag>></span><span>]<br> [</span><span class="tag"><</SPAN><SPAN class=attribute>ahref</SPAN><SPAN class=tag-name>ahref</SPAN><SPAN>='image.php'</SPAN><SPAN class=attribute-value>title</SPAN><SPAN>='浏览图片'</SPAN><SPAN class=tag>></span><span>浏览图片</span><span class="tag"></</SPAN><SPAN class=tag-name>a</SPAN><SPAN class=tag>></span><span>]</span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"></</SPAN><SPAN class=tag-name>tr</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"></</SPAN><SPAN class=tag-name>table</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"><span>"; </span></li> <li class=""> <span></span><span class="tag">?></span><span> </span> </li> </ol>