我正在尝试同时上传多张照片,但我的脚本似乎出现了错误。例如,如果我选择10张不同的照片,则会上传一张特定图像10次(忽略其他9张图像)。这是脚本:
for ($i = 0; $i < count($_FILES["_photo"]["name"]); $i++) {
if (!empty($_FILES['_photo']['name'][$i])) {
if (($_FILES['_photo']['type'][$i] == 'image/jpeg') OR ($_FILES['_photo']['type'][$i] == 'image/png') OR ($_FILES['_photo']['type'][$i] =='image/gif')) {
$upload_folder = "./profile_pix/";
$pic_name = time() . ".jpg";
$pic_path = $upload_folder . $pic_name;
require_once "include/resize.php";
if (move_uploaded_file($_FILES['_photo']['tmp_name'][$i], $pic_path)) {
$image = new Resize($pic_path);
$image->resizeImage(180, 180, 'crop');
$image->saveImage($pic_path);
}
$sql2 = "INSERT INTO photos
(photo, member_id, photo_id)
VALUES
('$pic_name', :session_id, :offer_count)";
$db -> query($sql2, array('session_id' => $_SESSION['id'], 'offer_count' => $offer_count));
}else {
header ("Location: submitoffer.php?err=03");
}
}
HTML:
<input type="file" id="_photo" name="_photo[]" multiple="multiple">
答案 0 :(得分:1)
文件上传工作正常。
该行
$pic_name = time() . ".jpg";
总是评估相同的值。
逻辑上,所有文件都上传到同一time()
。
改为使用:
$pic_name = $i . '_'. time() . ".jpg";
添加唯一性。
答案 1 :(得分:0)
试试这个,你会看到发生了什么:
<?php
echo "<pre>";
print_r($_FILES);
echo "</pre>";
?>
在HTML中,请确保使用不同的名称,如下所示:
<input name="userfile[]" type="file" />
<input name="userfile[]" type="file" />
<input name="userfile[]" type="file" />
然后按每个名字选择它们。只需检查$ _FILES的内容即可了解结构。
我也建议你认真做一些错误检查。
答案 2 :(得分:0)
100%正常工作且经过测试的代码:
<强> upload.php的:强>
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
<强> upload.html 强>
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
if(isset($_FILES['files'])){
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $_FILES['files']['name'][$key];
$file_size = $_FILES['files']['size'][$key];
$file_tmp = $_FILES['files']['tmp_name'][$key];
$file_type = $_FILES['files']['type'][$key];
if($file_type == "image/jpg" || $file_type == "image/png" || $file_type == "image/jpeg" || $file_type == "image/gif" ) {
move_uploaded_file($file_tmp,"uploads/".$file_name);
}
}
header('location:uploads.html');
}
}
else { header('location:404.html'); }
?>