Hello,

I tried to create a gallery using php admins can upload multiple images all the images are uploading correctly like the image is saving in the database as much as I can but the problem is that the image is saving once in the folder

Like loop runs images name saves in the database twice

Forexampl
img1.jpg
img2.jpg
img3.jpg

this is what comes in database but when i open the the folder i see
img1.jpg the rest are not coming

here is my code

$imggall = $_FILES["gallery_img"]["name"];
        $imgtype = $_FILES["gallery_img"]["type"];
        $imgtemp = $_FILES["gallery_img"]["tmp_name"];

        foreach($imggall as $key => $imgname) {

            $path = "./img/gallery/".$imgname;
            foreach($imgtemp as $key1 => $imgtemp1) {
                move_uploaded_file($imgtemp1, $path);
            }
            $confirm = mysqli_query($connect, "INSERT INTO gallery (image) VALUES ('$imgname')");
        }

Recommended Answers

All 15 Replies

Might wanna try this.

tried but not working can you help with my given code?

You appear to be using to many loops. Simplify your code, use the $_FILE array to insert file info in the database and folder. This should require only one loop to accomplish. Your sepearation of the $_FILE array ($imggall, $imgtype) serves no purpose, especially outside of your loop. check the file has been moved before inserting the info in the database.
is_file($path).
is_uploaded_file(tmp_name) for security.

Done but code is not working now the image is not inserting into the database or I didn't understood your words. Previously the code is working though but the images are not moving into the folder it moves once then nothing happens only one image inserts into the folder. Secondly like if I upload 4 images 4 of them will store into the database but only the first image will move into the folder.

Thank You

Your problem should be the part of

foreach($imggall as $key => $imgname) {
    $path = "./img/gallery/".$imgname;
    foreach($imgtemp as $key1 => $imgtemp1) {
        move_uploaded_file($imgtemp1, $path);
    }
    $confirm = mysqli_query($connect, "INSERT INTO gallery (image) VALUES ('$imgname')");
}

I assumed the values for

$imggall = $_FILES["gallery_img"]["name"];
$imgtype = $_FILES["gallery_img"]["type"];
$imgtemp = $_FILES["gallery_img"]["tmp_name"];

are something like this:

$imggall = array('1.jpg','2.png','3.jpg');
$imgtype = array('image/jpeg','image/png','image/jpeg');
$imgtemp = array('1.jpg','2.png','3.jpg');

Then my code will be

foreach($imggall as $key => $imgname) {
    $path = "./img/gallery/".$imgname;
    move_uploaded_file($imgtemp[$key], $path);
    $confirm = mysqli_query($connect, "INSERT INTO gallery (image) VALUES ('$imgname')");
}

nOW WHAT HAPPENS HERE I used your code and what happened is only 1 image is moving into the folder an nothing inserting in the database

no one here to help me out

Any one there to help no one is helping me I am stuck up please some one help me out

Any one there to help no one is helping me I am stuck up please some one help me out

Since you're not paying me (and everyone else here), we will respond when we can. Demanding help to just come isn't going to work, I'm afraid.

If only one file is moving, then clearly only one file is getting handled correctly into that array. Try a good ol' print_r($imggall) and print_r($imgtemp) just before the foreach so we can take a closer look.

If $imggall is being used as a key for $imgtemp, theres a risk that they may be different, so won't find each other or the desired file. (Basically dont count on that).

Using a loop and using the key is probably the most efficient way to work:

$i = 0;
foreach($imggall as $imgname) {
    $path = "./img/gallery/".$imgname;
    move_uploaded_file($imgtemp[$i], $path);
    $confirm = mysqli_query($connect, "INSERT INTO gallery (image) VALUES ('$imgname')");
    $i++;
}

@mattster I am exteremely sorry for the words I used as it was a pretty long time no one was helping me out so I was a bit disappointed I never said that about paying or any thing as this forum is just a community and teamly people and developers here help each other though even I will also do the same as 6 days i didn't got any response from any one though that is the reason paying is not a problem this forum was I beleived created for helping each other not for the reason of paying any one money is not the reason of everything and many things cannot be purchased ok.

So once again i am extremely sorry for something I have said wrong will not post anything or any others sorry for that

@UK-1991 that's fine, no hard feelings from me. No need to not post again aha, we're not that tight here. It was really the way you put your point across that might offend the people trying to help, and I'm sure you can appreciate most of us are very busy and checking DaniWeb often falls after many other things in our lives, so responces may well take a couple of days.

Moving on to a lighter note, did that help solve your problem?

commented: Well put +rep +15

Hello,
Thank you so much,

Well i tried to do print r and see what i got

Notice: Undefined variable: imgtemp in C:\wamp\www\comicsapp\admin\includes\functions.php on line

Notice: Undefined variable: imgtemp in C:\wamp\www\comicsapp\admin\includes\functions.php on line

My code

$imggall = $_FILES["gallery_img"]["name"];
    $imggall = $_FILES["gallery_img"]["type"];
    $imggall = $_FILES["gallery_img"]["tmp_name"];

    print_r($imggall);
    echo "<br />";
    $i = 0;
    foreach($imggall as $imgname) {
        $path = "./img/gallery/".$imgname;
        print_r($imgtemp);
        echo "<br />";

        print_r($imggall);
        echo "<br />";
        print_r($imgname);
        move_uploaded_file($imgtemp[$i], $path);
        $confirm = mysqli_query($connect, "INSERT INTO gallery (image) VALUES ('$imgname')");
        $i++;
    }

Output

Array
(
    [0] =&gt; C:\wamp\tmp\phpD00F.tmp
    [1] =&gt; C:\wamp\tmp\phpD010.tmp
)

Array
(
    [0] =&gt; C:\wamp\tmp\phpD00F.tmp
    [1] =&gt; C:\wamp\tmp\phpD010.tmp
)

C:\wamp\tmp\phpD00F.tmp

noticed your code as

$imggall = $_FILES["gallery_img"]["type"];
$imggall = $_FILES["gallery_img"]["tmp_name"];

where you replacing $imggall change the related one to $imgtemp.

Changed now I got this response

Warning: Invalid argument supplied for foreach() in C:\wamp\www\comicsapp\admin\includes\functions.php on line 520

check the variable provided to the foreach loop, confirm if the variable you want to supply is $imggall?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.