views:

78

answers:

2

I am using this peace of PHP code to save and rename some images.

foreach ($phones as &$value)
{
    $link_img =
    $handle = @fopen($link_img, "r");

    if ($handle) 
    {
        while (!feof($handle)) 
        {
            $buffer .= fgets($handle, 4096);
        }

        fclose($handle);
        $filename = "files/nokia_".$phone_model.".jpg";
        $mystring = fopen($filename, "wb");
        $handle = fopen($filename, "wb");
        $numbytes = fwrite($handle, $buffer);
        fclose($handle);
        unset($buffer);
    }
}

most of images are JPGs, i think that all files have extension .jpg, but one image I bump in to, have .jpg extension, but its gif (i think), ...

and then my foreach stops :(

how to handle that ?

thanks for your help, I learned much here at stackoverflow, and this is my first question.

A: 

Use the fileinfo functions instead of guessing what type the images are.

Ignacio Vazquez-Abrams
+1  A: 

All you need to do is update the following line:

$filename = "files/nokia_".$phone_model.".jpg";

To:

$filename = 'files/nokia_' . $phone_model . str_replace('jpeg', 'jpg', image_type_to_extension(exif_imagetype($link_img)));

I don't think this is the reason why your foreach loop is stopping though.

Alix Axel
seam that I don't have this function, trying with this right now http://www.php.net/manual/en/function.exif-imagetype.php#80383
Nemanja
now it works, and stops at different image, ... extension is not a problem, you were right.
Nemanja
@Nemanja: Try placing `set_time_limit(0);` at the top of your file and try again.
Alix Axel
it works now, thanks
Nemanja