在 PHP 中仅从 URL 获取带扩展名的文件名,而不是文件夹目录名称
我想使用 PHP 从当前 URL 获取带有扩展名的文件名。对于这个问题,网上有很多代码,但到目前为止,这些代码都不能满足我的需要。这是最常用的代码,但仍然没有满足我的要求。
好吧,我需要的是作为一个例子,我在下面共享了一些 URL,并希望获得所需的结果。
网址: https:// www.example.gov.us/directory_1/directory_2/directory_3/index.php
所需结果:index.php
网址: https://www.example.com/directory_1/directory_2/directory_3/index" example.gov.us/directory_1/directory_2/directory_3/index //通过.htaccess删除扩展名
所需成果:索引
网址:https://www.example .gov.us/directory_1/directory_2/directory_3/
所需结果: [空]
因此,为了这个目的,我使用了下面的代码...
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$path_parts = pathinfo($actual_link);
echo $path_parts['dirname'];
echo $path_parts['basename'];
echo $path_parts['extension'];
echo $path_parts['filename'];
但是我从上面的代码中收到了以下输出。
// Correct Output
$actual_link = "https://www.example.gov.us/directory_1/directory_2/directory_3/index.php";
$path_parts = pathinfo($actual_link);
echo $path_parts['dirname']; //https://www.example.gov.us/directory_1/directory_2/directory_3
echo $path_parts['basename']; //index.php
echo $path_parts['extension']; //php
echo $path_parts['filename']; //index
// Incorrect Output
$actual_link = "https://www.example.gov.us/directory_1/directory_2/directory_3/index";
$path_parts = pathinfo($actual_link);
echo $path_parts['dirname']; //https://www.example.gov.us/directory_1/directory_2/directory_3
echo $path_parts['basename']; //index
echo $path_parts['extension']; //Notice: Undefined index: extension
echo $path_parts['filename']; //index
// Incorrect Output
$actual_link = "https://www.example.gov.us/directory_1/directory_2/directory_3/";
$path_parts = pathinfo($actual_link);
echo $path_parts['dirname']; //https://www.example.gov.us/directory_1/directory_2
echo $path_parts['basename']; //directory_3
echo $path_parts['extension']; //Notice: Undefined index: extension
echo $path_parts['filename']; //directory_3
最后一种情况使此代码无法使用,因此是否有另一种选择可以在所有上面 3 个情况下获取带有扩展名的文件名?
I want to get the filename with an extension from the current URL using PHP. FOr this problem, there are many codes available online but none of those is covering my need till now. Here is the topmost used code that is still not giving me my requirement.
Well, what I need is as an example I have a few URLS shared below and want the desired outcome.
URL: https://www.example.gov.us/directory_1/directory_2/directory_3/index.php
Outcome Needed: index.php
URL: https://www.example.gov.us/directory_1/directory_2/directory_3/index //Extension removed via .htaccess
Outcome Needed: index
URL: https://www.example.gov.us/directory_1/directory_2/directory_3/
Outcome Needed: [Empty]
So for this purpose, I used the below code...
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$path_parts = pathinfo($actual_link);
echo $path_parts['dirname'];
echo $path_parts['basename'];
echo $path_parts['extension'];
echo $path_parts['filename'];
But I received the following outputs from the above code.
// Correct Output
$actual_link = "https://www.example.gov.us/directory_1/directory_2/directory_3/index.php";
$path_parts = pathinfo($actual_link);
echo $path_parts['dirname']; //https://www.example.gov.us/directory_1/directory_2/directory_3
echo $path_parts['basename']; //index.php
echo $path_parts['extension']; //php
echo $path_parts['filename']; //index
// Incorrect Output
$actual_link = "https://www.example.gov.us/directory_1/directory_2/directory_3/index";
$path_parts = pathinfo($actual_link);
echo $path_parts['dirname']; //https://www.example.gov.us/directory_1/directory_2/directory_3
echo $path_parts['basename']; //index
echo $path_parts['extension']; //Notice: Undefined index: extension
echo $path_parts['filename']; //index
// Incorrect Output
$actual_link = "https://www.example.gov.us/directory_1/directory_2/directory_3/";
$path_parts = pathinfo($actual_link);
echo $path_parts['dirname']; //https://www.example.gov.us/directory_1/directory_2
echo $path_parts['basename']; //directory_3
echo $path_parts['extension']; //Notice: Undefined index: extension
echo $path_parts['filename']; //directory_3
The last scenario makes this code un-usable so is there an alternative where I can get the file name with extension in all upper 3 scenarios?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是目录的基本名称就是目录。
我认为您需要针对第三种情况的一些自定义逻辑
str_ends_with() 是 PHP 8 函数。如果您需要旧版本的支持,请使用这个polyfill
Demo ~ https://3v4l.org/qQiOq
The problem is that the base-name for a directory, is the directory.
I think you'd need some custom logic for that 3rd case
str_ends_with() is a PHP 8 function. If you need support for older versions, use this polyfill
Demo ~ https://3v4l.org/qQiOq