php - PHP将多个图像上传到目录 - 失败
问题描述
所以我有这个正在工作的脚本,它将图像上传到正确的目录(我创建的)并将图像放入该目录并重命名它们,酷!但是,我放置的小显示错误文本总是打印每一个错误。因此,例如我有 $j++ 并且我的错误最多将有 $j = 4 并且如果 $j = 4 那么所有错误都会打印出来,你会假设有问题并且图像还没有上传。但是我选择了 4 张图片,其中 3 张上传了,但仍然显示所有错误。
所以我的问题是,到底为什么会显示所有错误以及为什么只有四分之三的图片上传?我希望能够一次上传 30 张图片...
代码
//Image Upload Section
$j = 0; //Variable for indexing uploaded image
$target_path = $imagePath."/"; //Declaring Path for uploaded images
for ($i = 0; $i < count($_FILES['file']['name']); $i++) { //loop to get individual element from the array
$validextensions = array("jpeg", "jpg", "png"); //Extensions which are allowed
$ext = explode('.', basename($_FILES['file']['name'][$i])); //explode file name from dot(.)
$file_extension = end($ext); //store extensions in the variable
$target_path = $target_path.md5(uniqid()).
".".$ext[count($ext) - 1]; //set the target path with a new name of image
$j = $j + 1; //increment the number of uploaded images according to the files in array
if (($_FILES["file"]["size"][$i] < 100000000) //Approx. 100000kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) { //if file moved to uploads folder
echo $j.
').<span id="noerror">Image Uploaded Successfully!.</span><br/><br/>';
} else { //if file was not moved.
echo $j.
').<span id="error">Please Try Again!.</span><br/><br/>';
}
} else { //if file size and file type was incorrect.
echo $j.
').<span id="error">***Invalid File Size or Type***</span><br/><br/>';
}
}
//End Image Upload Section
PS 附带说明一下,是否有一种简单的方法可以遍历目录并选择其中的所有图像,然后以 HTML 格式显示它们?
解决方案
推荐阅读
- tensorflow-federated - TFF :每个客户端都执行预训练函数而不是 build_federated_averaging_process
- swift - 如何在集合视图中快速创建具有倾斜效果的捕捉滑块?
- mysql - Attunity 零时间戳值插入 mySQL DB
- boost - 构建一个使用本地发行版的 boost 的 cmake 项目
- android - 将数据从 BottomSheetFragment 传递到前一个 Fragment
- c# - 将泛型类型提取到接口中
- installation - CVI 安装
- assembly - 不明白如何完成这个 ARM 汇编代码来做 5 个数字的总和和余数的平均值
- html - 如何为网站水平堆叠 CSS 元素
- node.js - 请求成功,但是表单提交的字段数据没有提交到数据库中?