我想从给定的网址检索图像,我有文件名,但问题是扩展名未知。它可以是 jpg、gif 或 png。
这就是我的处理方法,但似乎不起作用:
$extensions = array('jpg', 'png', 'gif');
foreach ($extensions as $ext) {
if (file_exists('https://mytestimage.com/products/3c763dce-666b-4ba9-a05a-cca846391aea/thumb.' . $ext)) {
echo 'Found filename.' . $ext;
break;
}
}
有什么建议吗?
最佳答案
由于文件不在现场,请检查 URL 是否存在。
$file = 'http://example.com/image.jpg';
$fileHeaders = @get_headers($file);
if( $fileHeaders[0]=='HTTP/1.1 404 Not Found')
{
// File doesn't exist
}
else
{
// File exists
}
您可以将此逻辑应用于不同扩展的循环。
关于php如何在不知道扩展名的情况下从url获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30329021/