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条)

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏