dtn36013 2014-07-10 14:09
浏览 36
已采纳

PHP从图像URL获取文件扩展名未以.ext结尾

pathinfo() works fine only when the extension is present.

WORKS FINE:

$sourceUrl = "site.com/image.png";    
$photoExt = pathinfo($sourceUrl, PATHINFO_EXTENSION);

.

NOT WORKING

$sourceUrl = "www.gravatar.com/avatar/d9d3bffa0ca503c495b2461881688467?s=32&d=identicon&r=PG";    
$photoExt = pathinfo($sourceUrl, PATHINFO_EXTENSION);

I couldn't see solution on how to get file/image extension when the source doesn't ended on it's file type.

Any solution?

  • 写回答

2条回答 默认 最新

  • dop20345 2014-07-10 14:19
    关注

    You cannot use pathinfo to get extension of file without extension.

    For this remote file we only know what's the content-type of this image (Content-Type => image/jpeg). But this content-type can have jpg or jpeg extension and we simple don't know what's this file real extension so PHP cannot determine it either.

    If you want to simple check image type of this resource you can use:

    echo exif_imagetype('http://www.gravatar.com/avatar/d9d3bffa0ca503c495b2461881688467?s=32&d=identicon&r=PG');
    

    It will return 2, what is IMAGETYPE_JPEG according to documentation but still you won't know anything about real file extension.

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

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面