0

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.

9
  • check what move_uploaded_file() is returning. Commented Feb 13, 2012 at 19:45
  • echo move_uploaded_file($_FILES['file']['tmp_name']); Did this then exited and nothing displayed on screen, is there a different way to check? Commented Feb 13, 2012 at 19:49
  • Are you sure that you have the correct permissions to write to that directory? Also, replace $_FILES[file][tmp_name] with $_FILES['file']['tmp_name'] "file" is a function in PHP, and you should always enclose textual indexes in quotes. Perhaps PHP runs the "file" function and uses the return value as an index (not what you want) Commented Feb 13, 2012 at 19:49
  • Yes I am able to write to directory, I have been using a single file uploader and now I am turning it into a multiple along with thumbnail creation. Commented Feb 13, 2012 at 19:50
  • Try replacing $_FILES[file][tmp_name] with $_FILES['file']['tmp_name']. Even if it doesn't help, it is good practice to do so Commented Feb 13, 2012 at 19:51

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

you should always use that anyway, fix others too
Gaurav, that worked it uploaded the file into the correct directory, but the thumbnail creation did not work.
in for loop where ever you are using $_FILES['file']['type'] or any thing related to $_FILES you need to use $_FILES['file']['type'][$key] also
I was doing that just now actually and it worked, thanks for the fix. It's the obvious things sometimes that just pass right by!

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.