Php 如何从url获取图像的名称?

Php 如何从url获取图像的名称?,php,curl,Php,Curl,我有一个小问题;在PHP中,我使用curl从URL获取数据: $url = "http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg"; 我使用了curl\u getinfo(),它给了我一个数组: Array ( [url] => http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg [content_type

我有一个小问题;在PHP中,我使用curl从URL获取数据:

$url = "http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg";
我使用了
curl\u getinfo()
,它给了我一个数组:

Array
(
[url] => http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg
[content_type] => image/jpeg
[http_code] => 200
[header_size] => 496
[request_size] => 300
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 2.735
[namelookup_time] => 0.063
[connect_time] => 0.063
[pretransfer_time] => 0.063
[size_upload] => 0
[size_download] => 34739
[speed_download] => 12701
[speed_upload] => 0
[download_content_length] => 34739
[upload_content_length] => -1
[starttransfer_time] => 1.282
[redirect_time] => 0
)
如何在链接
[url]=>http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg
例如

[image_name] : example
[image_ex] : jpg

谢谢你的建议

使用

考虑以下是图像路径 $image_url=''; 使用以下函数basename()从该url获取扩展名为的图像名称; 看看下面的代码

代码:

输出:
attorney1.png

有时url会附加额外的参数。在这种情况下,我们可以先删除参数部分,然后使用PHP内置的pathinfo()函数从url获取图像名称

$url = 'http://images.fitnessmagazine.mdpcdn.com/sites/story/shutterstock_65560759.jpg?itok=b8HiA95H';
检查图像url是否附加了参数

if (strpos($url, '?') !== false) {
    $t = explode('?',$url);
    $url = $t[0];            
}      
结果url变量现在包含

http://images.fitnessmagazine.mdpcdn.com/sites/story/shutterstock_65560759.jpg

使用以检索所需的详细信息

$pathinfo = pathinfo($url);
echo $pathinfo['filename'].'.'.$pathinfo['extension'];
这将提供shutterstock_65560759.jpg作为输出。

您可以使用正则表达式
/(?:.+\/)(.+\.(png | jpg | jepg))[?#].$/

例如:

$examples=[
'http://images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?itok=b8HiA95H',
'http://images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#hghgh',
'http://images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?',
'http://images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#',
'http://images.fitnessmagazine.mdpcdn.com/sites/story/with_multydots.65560759.jpg',
'http://images.fitnessmagazine.mdpcdn.com/sites/story/image.png',
'http://images.fitnessmagazine.mdpcdn.com/sites/story/without_ext',   
];
foreach($examples作为$example){
preg#u match('/(?:.+\/)(.+\.(png | jpg | jepg))[?#]?.*$/',$example,$matches);
if(isset($matches[1])&&$matches[1]){
echo“Url:{$example},图像名称:{$matches[1]}\n”;
}否则{
echo“Url:{$example}不是图像Url\n”;
}
}
印刷品:

Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?itok=b8HiA95H, image name: with_query.jpg 
Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#hghgh, image name: with_hash.jpg 
Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?, image name: with_query.jpg 
Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#, image name: with_hash.jpg 
Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_multydots.65560759.jpg, image name: with_multydots.65560759.jpg 
Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/image.png, image name: image.png 
Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/without_ext is not image url 

如果要从忽略uri参数的url获取文件名,可以尝试以下操作:

$url = 'http://host/filename.ext?params';
$parsedUrl = parse_url($url);
$pathInfo = pathinfo($parsedUrl['path']);
print_r($pathInfo);
Array
(
    [dirname] => /
    [basename] => filename.ext
    [extension] => ext
    [filename] => filename
)


严格来说,这不是图像的名称,尽管大多数浏览器在大部分时间保存图像时都会使用这个名称,因为web上的大多数实体没有名称,但这可能是一个不错的选择。如果prelovac.com的负责人真的想设置一个名称,他们会使用contentdisposition标题。虽然浏览器仍然可以忽略这一点,但如果您想匹配大多数用户将看到的保存为的文件,然后首先检查URI的路径信息,如果没有,只检查URI的路径信息。有一些PHP函数可以一次完成这项工作。尝试添加一些解释来提高您的URI的质量answer@Sinha这正是我一直在寻找的,非常感谢你的回答。@ArakTai'Roth,很高兴知道这对你有帮助。:)对于那些没有从上面获得工作链接的用户,它是
if (strpos($url, '?') !== false) {
    $t = explode('?',$url);
    $url = $t[0];            
}      
$pathinfo = pathinfo($url);
echo $pathinfo['filename'].'.'.$pathinfo['extension'];
Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?itok=b8HiA95H, image name: with_query.jpg 
Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#hghgh, image name: with_hash.jpg 
Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?, image name: with_query.jpg 
Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#, image name: with_hash.jpg 
Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_multydots.65560759.jpg, image name: with_multydots.65560759.jpg 
Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/image.png, image name: image.png 
Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/without_ext is not image url 
$url = 'http://host/filename.ext?params';
$parsedUrl = parse_url($url);
$pathInfo = pathinfo($parsedUrl['path']);
print_r($pathInfo);
Array
(
    [dirname] => /
    [basename] => filename.ext
    [extension] => ext
    [filename] => filename
)