klye_g
klye_g

Reputation: 1242

PHP Multiple file upload and create thumbnails not getting into for each loop

Here is what I have so far, I apologize for the length but this gives you the complete picture:

if(count($_FILES['file']['name'])) 
            {

            foreach ($_FILES['file']['name'] as $key => $file)
                {



            $add="../uploads/photogallery/".time().'_'.$file;

            $storefile = time().'_'.$file;

                             echo $storefile; // THIS WORKS

                if(move_uploaded_file ($_FILES['file']['tmp_name'][$key],$add))
                {

                                     // CODE IS NOT GETTING INTO THIS AREA,

                    //database insert here

                    //mysql_query ($query);

                    echo '<meta http-equiv="refresh" content="0;url=?VIEW=PHOTOADD&photo_added">';
                }
                else
                {
                    echo '<meta http-equiv="refresh" content="0;url=?VIEW=PHOTOADD&photo_not_added">';
                }

            ///////// Start the thumbnail generation//////////////

            $n_width=150;          // Fix the width of the thumb nail images
            $n_height=150;         // Fix the height of the thumb nail imaage

            $tsrc="../uploads/photogallery/thumbnail/".time().'_'.$file;

            if (!($_FILES['file']['type'][$key] =="image/pjpeg" || $_FILES['file']['type'][$key] == "image/gif" || $_FILES['file']['type'][$key] == "image/png") || ($_FILES["file"]["type"][$key] == "image/jpeg") && ($_FILES["file"]["size"][$key] < 2097152))
                {
                    echo '<meta http-equiv="refresh" content="0;url=?VIEW=PHOTOADD&photo_requirements_not_met">';
                }

            /////////////////////////////////////////////// Starting of GIF thumb nail creation///////////

            if (@$_FILES['file']['type'][$key] =="image/gif")
                {
                    $im=ImageCreateFromGIF($add);

                    $width=ImageSx($im);              // Original picture width is stored
                    $height=ImageSy($im);                  // Original picture height is stored

                    $newimage=imagecreatetruecolor($n_width,$n_height);

                    imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);

                    if (function_exists("imagegif")) 
                    {
                        Header("Content-type: image/gif");
                        ImageGIF($newimage,$tsrc);
                    }

                    elseif (function_exists("imagejpeg")) 
                    {
                        Header("Content-type: image/jpeg");
                        ImageJPEG($newimage,$tsrc);
                    }
                    elseif (function_exists("imagepng")) 
                    {
                        Header("Content-type: image/png");
                        ImagePNG($newimage,$tsrc);
                    }
                }

            ////////////// starting of JPG thumb nail creation//////////

            if ($_FILES['file']['type'][$key] == "image/pjpeg")
                {
                    $im=ImageCreateFromJPEG($add); 

                    $width=ImageSx($im);              // Original picture width is stored
                    $height=ImageSy($im);             // Original picture height is stored

                    $newimage=imagecreatetruecolor($n_width,$n_height);                 

                    imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);

                    ImageJpeg($newimage,$tsrc);
                }

            if ($_FILES['file']['type'][$key] == "image/png")
                {
                    $im=ImageCreateFromPNG($add); 

                    $width=ImageSx($im);              // Original picture width is stored
                    $height=ImageSy($im);             // Original picture height is stored

                    $newimage=imagecreatetruecolor($n_width,$n_height);                 

                    imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);

                    ImagePNG($newimage,$tsrc);
                }
            }
        }
        else
        {
            echo '<meta http-equiv="refresh" content="0;url=?VIEW=PHOTOADD&photo_requirements_not_met">';
        }

And here is the HTML portion of the form:

<input class="input" name="file[]" type="file" id="file" multiple=""/>

Yes the form has the correct enctype. See my comments in the code above where it says THIS WORKS and CODE IS NOT..etc. That is where my issue is. It's getting inside the for each loop part since I can echo $storefile and get the correct filename with the time stamp, but it won't get into the if (move_uploaded_..) portion and skips to the echo photo requirements not met portion.

Any help would be greatly appreciated. I am not getting any PHP or mysql errors.

Upvotes: 0

Views: 1063

Answers (1)

Gaurav
Gaurav

Reputation: 28755

use foreach ($_FILES['file']['name'] as $key => $file) as for loop

$_FILES['file']['tmp_name'][$key] in if clause

for reference

For instance, assume that the filenames /home/test/review.html and /home/test/xwp.out are submitted. In this case, $_FILES['userfile']['name'][0] would contain the value review.html, and $_FILES['userfile']['name']1 would contain the value xwp.out. Similarly, $_FILES['userfile']['size'][0] would contain review.html's file size, and so forth.

$_FILES['userfile']['name'][0], $_FILES['userfile']['tmp_name'][0], $_FILES['userfile']['size'][0], and $_FILES['userfile']['type'][0] are also set.

Upvotes: 2

Related Questions