bonaca bonaca - 1 year ago 126
PHP Question

How to get file name from url without $_GET variable?


$address = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$stack = explode('/', $_SERVER["REQUEST_URI"]);
$file = array_pop($stack);
echo $file;

result -

How to get just file name (
) without
variable, using
if possible?

Answer Source

I will follow parse_url() like below (easy to understand):-

$url = 'http://localhost/mc/site-01-up/index.php?c=lorem-ipsum';

$url=   parse_url($url);
print_r($url); // to check what parse_url () will outputs
$url_path = explode('/',$url['path']); // explode the path part
$file_name =  $url_path[count($url_path)-1]; // get last index value which is your desired result

echo $file_name;


Note:- tested with your given URL. Check for other type of URL's at your end. thanks.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download