0

I am working on an image upload script and ran into this problem. Using ImageMagick I would run out of time resizing and uploading images so I inserted these two lines:

set_time_limit(120);
ini_set('max_input_time', 120);

Now however, the script never ends. It is continually running the page even though it uploads, resizes, and inserts to the database fine. Do these conflict with each other in some way? It even goes past the 120 seconds I set as the time limit.

Edit: This is the full image editing portion of the script, it works with say 1 or 2 images but when I put in more (thereby making it take longer) it does not.

set_time_limit(120);
ini_set('max_input_time', 120);
$resource = NewMagickWand(); 
MagickReadImage($resource,$image); 
MagickSetImageCompressionQuality( $resource, 100);
$resource = MagickTransformImage($resource,'0x0','660x500'); 
MagickWriteImage($resource, $image);
DestroyMagickWand($resource);

This is the code I use to read all the images that are to be uploaded: (reSizePic is the function that calls the code above)

$numberImages = count($_FILES['galFile']['name'])-1;

    for($i=1;$i<=$numberImages;$i++)
    {
    $imageName = $_FILES['galFile']['name'][$i];
                $imageType = $_FILES['galFile']['type'][$i];
                $imageSize = $_FILES['galFile']['size'][$i];
                $imageTemp = $_FILES['galFile']['tmp_name'][$i];
                $imageError = $_FILES['galFile']['error'][$i];

                //Make sure it is an image
                if(in_array(end(explode(".", $imageName)), $allowed))
                {
                     //Where to upload image to
                     $uploadFile = $uploadDir . $imageName;
                     if (file_exists($uploadFile))
            {
                //What to do if file already exists
                //Append random number to the end
                $front = explode(".", $imageName);
                $randomNum = rand(1,100);
                $front[0] = $front[0].$randomNum;
                $imageName = $front[0].".".$front[1];
                $uploadFile = $uploadDir . $imageName;
            }
                      if(move_uploaded_file($imageTemp,$uploadFile))
                      {
                      //Add $imageName to DB
                       $query = "INSERT INTO galleryImages VALUES(\"0\",\"$lastInsert\",\"$imageName\",\"$i\")";
                   mysql_query($query);
                   reSizePic($uploadFile);
                      }
                }



Levi

Levi
  • 12,214
  • 14
  • 43
  • 47
  • can you show the php code that you're using in your script? – Aziz Apr 04 '09 at 22:00
  • I added in the rest of the image function. – Levi Apr 04 '09 at 22:05
  • try set_time_limit(0); ? – Aziz Apr 04 '09 at 22:09
  • Doesn't that just make it an infinite time limit? How would taking the time limit out stop it from looping? – Levi Apr 04 '09 at 22:14
  • ok, my bad, my thoughts went the wrong direction :P. Are you uploading the images, reading them from the same machine or from a remote host? – Aziz Apr 04 '09 at 22:16
  • I am uploading them to my web site, then resizing them. That works fine, it is just not ending. After say a minute or so of work I check via ftp for the images and they are there but the script will continue to run until I stop it. – Levi Apr 04 '09 at 22:19
  • I assume you have a loop to go through all images, can you put the code for it? (my guess is that the loop goes infinitely) – Aziz Apr 04 '09 at 22:21
  • I added it, but the thing is that it works fine so long as I don't add in too many images. – Levi Apr 04 '09 at 22:26
  • I know this is not the problem, but shouldn't the loop start at 0? – Aziz Apr 04 '09 at 22:33
  • It would normally, however the way I am storing multiple images on the client side before uploading them it always ends up with one blank image. So I just start at 1 to bypass it. – Levi Apr 04 '09 at 23:26

1 Answers1

2

You should make sure to only call set_time_limit() once, as each time you call it, the timer will be reset.

For instance, if you call set_time_limit(30) 10 seconds in to your script, your script will run for a total of 40 seconds. So setting it on each resizePic() call is a bad idea.

zombat
  • 92,731
  • 24
  • 156
  • 164
  • If I take them out of the loop I get an Internal Server Error after it uploads like 10 images out of 15. – Levi Apr 05 '09 at 04:37
  • try to put it at the beginning of the script, and set the timeout to 0 (infinite) – Aziz Apr 05 '09 at 10:38
  • Didn't work, but what I did notice that I didn't before was that only some of the pictures were being resized. They get uploaded and then resized so I need to figure out how to resize then upload them. I think the server has a time limit I can't override. – Levi Apr 05 '09 at 16:42
  • I don't think it is possible resize the image before it is uploaded. Try to show some output in the resize() function in order to see which function is the one that stops. – Aziz Apr 05 '09 at 17:35