【问题标题】:Error in uploading multiple images上传多张图片时出错
【发布时间】:2018-03-14 06:05:48
【问题描述】:

我正在尝试一次上传多张图片,这是我目前所拥有的:

if(isset($_POST['submit']))
{

        $file_name=$_FILES["image"]["name"];

            foreach($file_name as $files)
            {
                $target_path = "Sub_uploads/".$files;

                if(move_uploaded_file($files["image"]["tmp_name"],$target_path)) 
                {   

                    $target_path="Sub_uploads/".$files;

                    $sql   = "INSERT INTO product_images (image) VALUES ('$target_path')"; 
                    $query = mysql_query($sql); 
                }
            }
             echo "<script>alert('data inserted');document.location='Sub_CateGory_image.php'</script>";
}
?>

似乎是在这一行出现错误:if(move_uploaded_file($files["image"]["tmp_name"],$target_path))

【问题讨论】:

  • 请说明报告的错误。
  • 只需将 $_FILES["image"]["tmp_name"] 替换为 $files["image"]["tmp_name"]
  • 我试试这个,但它不起作用..

标签: php image file-upload image-uploading


【解决方案1】:

你需要使用ForEach$files的变量

if(isset($_POST['submit']))
{

        $file_name=$_FILES;

            foreach($file_name as $files)
            {
                $target_path = "Sub_uploads/".$files["image"]["name"];

                if(move_uploaded_file($files["image"]["tmp_name"],$target_path)) 
                {   

                    $target_path="Sub_uploads/".$files["image"]["name"];

                    $sql   = "INSERT INTO product_images (image) VALUES ('$target_path')"; 
                    $query = mysql_query($sql); 
                }
            }
             echo "<script>alert('data inserted');document.location='Sub_CateGory_image.php'</script>";
}

【讨论】:

  • $files 是一个字符串。
  • 如果您不知道$_FILES 数组的结构,请不要回答。您编辑的答案仍然不正确
【解决方案2】:

当您迭代 name 数组时,连接的 tmp_name 属性将与当前迭代的 name 具有相同的键。因此,将key 添加到您的foreach 并在此键下获得tmp_name

$file_name = $_FILES["image"]["name"];
foreach($file_name as $key => $files)   // add `$key` here
{
    $target_path = "Sub_uploads/".$files;
    // use `$key` to get connected `tmp_name`
    if(move_uploaded_file($_FILES["image"]["tmp_name"][$key], $target_path)) 
    {   
        $target_path="Sub_uploads/".$files;
        $sql   = "INSERT INTO product_images (image) VALUES ('$target_path')"; 
        $query = mysql_query($sql); 
    }
}

【讨论】:

    猜你喜欢
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    相关资源
    最近更新 更多