0

I am trying to upload multiple images at once here is what i've got so far:

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>";
}
?>

It seems so that the error occurs at this line: if(move_uploaded_file($files["image"]["tmp_name"],$target_path))

3
  • Please specify the error reported. Commented Mar 14, 2018 at 6:10
  • Just replace $_FILES["image"]["tmp_name"] with $files["image"]["tmp_name"] Commented Mar 14, 2018 at 6:13
  • i try this but it is not work.. Commented Mar 14, 2018 at 6:27

2 Answers 2

1

You need to use variable of 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>";
}
Sign up to request clarification or add additional context in comments.

2 Comments

$files is a string.
Please, if you don't know the structure of $_FILES array - don't answer. Your edited answer is still incorrect.
0

As you iterate over array of name, the connected tmp_name property will have the same key as current iterated name. So, add a key to your foreach and get a tmp_name under this key:

$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); 
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.