簡體   English   中英

上傳多張圖片時出錯

[英]Error in uploading multiple images

我正在嘗試一次上傳多張圖片,這是到目前為止的內容:

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))

您需要使用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>";
}

當您迭代name數組時,連接的tmp_name屬性將具有與當前迭代name相同的鍵。 因此,向您的foreach添加一個key ,並在此密鑰下獲得一個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); 
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM