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

报告相同问题?