dsfykqq3403 2010-01-25 01:18
浏览 89
已采纳

从服务器保存图像时,文件格式错误(gif而不是jpg)问题 - PHP

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.

  • 写回答

2条回答 默认 最新

  • douyuan4825 2010-01-25 01:34
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效