Get entire file and extensions name from URL

Hi All

I am using PHP 7.3 and am struggling to find the right code to find the right code to get my filename and all extension info from the URL.

An example URL is:

I want to get:


Can anyone help me out or point me in the right direction please.


Hi @mrmbarnes I think something like the following should do the trick (hopefully as I haven’t tested it)

    $url = $_SERVER['REQUEST_URI'];
    $urlParts = explode('/', $url);
    $urlString = array_pop($urlParts);
    echo $urlString;

Thanks… how do I get the URL from the browser tab?

The answer is simple and I’m tempted to give it to you, but it is so simple you should probably google it! :wink:

Edit: updated code in answer above

Thanks… for anyone else who might find this thread this is what worked for me in the end:

<?php $url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $urlParts = explode('/', $url); $urlString = array_pop($urlParts); echo $urlString; ?>

Or you could use parse_url.


Yeah thought about it too, but it doesn’t seem that wil return exactly the fragment he needs…


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.