Home > front end >  Get Only File Name With Extension From URL Not Folder Directory Name In PHP
Get Only File Name With Extension From URL Not Folder Directory Name In PHP

Time:03-14

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?

CodePudding user response:

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

$outcome = str_ends_with($actual_link, "/") ? "" : basename($actual_link);

str_ends_with() is a PHP 8 function. If you need support for older versions, use this polyfill

if (!function_exists("str_ends_with")) {
    function str_ends_with($haystack, $needle) {
        return strrpos($haystack, $needle) === strlen($haystack) - strlen($needle);
    }
}

Demo ~ https://3v4l.org/qQiOq

  • Related