php – 多个图像上传不在目录上移动文件
发布时间:2020-12-13 22:46:46 所属栏目:PHP教程 来源:网络整理
导读:尝试构建一个多图像上传,从我的表单中获取3个提交的文件并将它们存储在我的服务器上. 我有以下但无论我收到什么“无效的文件”可以anybdoy看到我出错的地方? for($i = 0; $i 3; $i++) { $aFile = $_FILES['file'][$i]; $allowedExts = array("jpg","jpeg","
尝试构建一个多图像上传,从我的表单中获取3个提交的文件并将它们存储在我的服务器上.
我有以下但无论我收到什么“无效的文件”可以anybdoy看到我出错的地方? for($i = 0; $i < 3; $i++) { $aFile = $_FILES['file'][$i]; $allowedExts = array("jpg","jpeg","gif","png"); $extension = end(explode(".",$aFile["file"]["name"])); if ((($aFile["file"]["type"] == "image/gif") || ($aFile["file"]["type"] == "image/jpeg") || ($aFile["file"]["type"] == "image/png") || ($aFile["file"]["type"] == "image/pjpeg")) && ($aFile["file"]["size"] < 20000) && in_array($extension,$allowedExts)) { if ($aFile["file"]["error"] > 0) { echo "Return Code: " .$aFile["file"]["error"] . "<br>"; } else { if (file_exists("upload/" . $aFile["file"]["name"])) { echo $aFile["file"]["name"] . " already exists. "; } else { move_uploaded_file($aFile['tmp_name'],"upload/" . date('U')."-".$aFile["file"]["name"]); // add a unique string to the uploaded filename so that it is unique. echo "Image Uploaded Successfully"; } } } else { echo "Invalid file"; } } ** HTML ** <li> <label for="picture_1">picture 1 : </label> <input type="file" name="file"> </li> <li> <label for="picture_2">picture 2 : </label> <input type="file" name="picture_2"> </li> <li> <label for="picture_3">picture 3 : </label> <input type="file" name="picture_3"> </li> 解决方法
我猜,主要问题是您的HTML元素名称和$_FILES中使用的名称无法匹配,即您使用了第一个文件输入名称“file”.它应该是“picture_1”.并且您在文件处理部分使用了索引0到2.匹配“picture_1”,“picture_2”和“picture_3”应为1到3.
请注意,您的表单应具有enctype =“multipart / form-data”,否则您的文件将不会上传.这是正确的: 有两种方法可以实现这一点: 方法1:单独命名文件输入 HTML <form method="post" enctype="multipart/form-data"> <li> <label for="picture_1">picture 1 : </label> <input type="file" name="picture_1" id="picture_1" /> </li> <li> <label for="picture_2">picture 2 : </label> <input type="file" name="picture_2" id="picture_2" /> </li> <li> <label for="picture_3">picture 3 : </label> <input type="file" name="picture_3" id="picture_3" /> </li> <input type="submit" name="submit" value="Upload" /> </form> PHP if(sizeof($_FILES)){ for($i = 1; $i <= 3; $i++) { $aFile = $_FILES['picture_'.$i]; if(empty($aFile['tmp_name'])) continue; # skip for empty elements $allowedExts = array("jpg","png"); $extension = end(explode(".",$aFile["name"])); if ((($aFile["type"] == "image/gif") || ($aFile["type"] == "image/jpeg") || ($aFile["type"] == "image/png") || ($aFile["type"] == "image/pjpeg")) && ($aFile["size"] < 20000) && in_array(strtolower($extension),$allowedExts)) { if ($aFile["error"] > 0) { echo "Return Code: " .$aFile["error"] . "<br>"; } else { if (file_exists("upload/" . $aFile["name"])) { echo $aFile["name"] . " already exists. "; } else { move_uploaded_file($aFile['tmp_name'],"upload/" . date('U')."-".$aFile["name"]); echo "Image Uploaded Successfully"; } } } else { echo "Invalid file"; } } } 方法2:将文件输入命名为一组 HTML <form method="post" enctype="multipart/form-data"> <li> <label for="picture_1">picture 1 : </label> <input type="file" name="file[]" id="picture_1" /> </li> <li> <label for="picture_2">picture 2 : </label> <input type="file" name="file[]" id="picture_2" /> </li> <li> <label for="picture_3">picture 3 : </label> <input type="file" name="file[]" id="picture_3" /> </li> <input type="submit" name="submit" value="Upload" /> </form> PHP if(sizeof($_FILES)){ for($i = 0; $i < 3; $i++) { $name = $_FILES['file']['name'][$i]; $type = $_FILES['file']['type'][$i]; $tmp_name = $_FILES['file']['tmp_name'][$i]; $error = $_FILES['file']['error'][$i]; $size = $_FILES['file']['size'][$i]; if(empty($name)) continue; # skip for empty element $allowedExts = array("jpg",$name)); if (( ($type == "image/gif") || ($type == "image/jpeg") || ($type == "image/png") || ($type == "image/pjpeg")) && $size < 20000 && in_array(strtolower($extension),$allowedExts) ) { if ($error > 0) { echo "Return Code: " .$error . "<br>"; } else { if (file_exists("upload/" . $name)) { echo $aFile["file"]["name"] . " already exists. "; } else { move_uploaded_file($tmp_name,"upload/" . date('U')."-".$name); echo "Image Uploaded Successfully"; } } } else { echo "Invalid file"; } } } 鸣谢: >应使用小写检查文件扩展名 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |