如何从AWS URL中提取文件名?

时间:2019-07-15 12:48:55

标签: php

我正在尝试从URL获取文件名,在这里我使用basename来获取文件名,但是它不起作用。

ximagesrc

结果是

basename('https://test-assets.amazonaws.com/live%2Ftestsamantha-9-bucket%2Ff132eb33-f403-4881-80e2-de1cc3bdb43a.jpg');

我如何获得

live%2Ftestsamantha-9-bucket%2Ff132eb33-f403-4881-80e2-de1cc3bdb43a.jpg

2 个答案:

答案 0 :(得分:3)

您照原样使用该网址,并在其上使用urldecode,因此斜杠是普通斜杠,没有任何编码。然后,您所需要做的就是用斜线explode并取所创建数组的最后一个元素。瞧!

$url = urldecode(basename('https://test-assets.amazonaws.com/live%2Ftestsamantha-9-bucket%2Ff132eb33-f403-4881-80e2-de1cc3bdb43a.jpg'));
$parts = explode('/', $url);

print end($parts); // Prints 'f132eb33-f403-4881-80e2-de1cc3bdb43a.jpg'

或者更好,像其他海报一样做,它的代码更少,他是对的。虽然我的解决方案也可以工作,但超出了需要。

答案 1 :(得分:2)

您首先需要使用urldecode方法对网址进行解码:

if !flag_display
    +details(details)

basename(urldecode('https://test-assets.amazonaws.com/live%2Ftestsamantha-9-bucket%2Ff132eb33-f403-4881-80e2-de1cc3bdb43a.jpg')); 方法将URL转换为urldecode,然后https://test-assets.amazonaws.com/live/testsamantha-9-bucket/f132eb33-f403-4881-80e2-de1cc3bdb43a.jpg方法正常工作。

相关问题