Get image name from url php

You can use regex /[?:.+\/][.+\.[png|jpg|jepg]][?#]?.*$/

Example:

$examples = [
    '//images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?itok=b8HiA95H',
    '//images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#hghgh',
    '//images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?',
    '//images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#',
    '//images.fitnessmagazine.mdpcdn.com/sites/story/with_multydots.65560759.jpg',
    '//images.fitnessmagazine.mdpcdn.com/sites/story/image.png',
    '//images.fitnessmagazine.mdpcdn.com/sites/story/without_ext',   
    ];

foreach[$examples as $example] {
    preg_match['/[?:.+\/][.+\.[png|jpg|jepg]][?#]?.*$/', $example, $matches];
    
    if[isset[$matches[1]] && $matches[1]] {
     echo "Url: {$example}, image name: {$matches[1]} \n";   
    } else {
        echo "Url: {$example} is not image url \n";   
    }
}


Printed:

Url: //images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?itok=b8HiA95H, image name: with_query.jpg 
Url: //images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#hghgh, image name: with_hash.jpg 
Url: //images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?, image name: with_query.jpg 
Url: //images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#, image name: with_hash.jpg 
Url: //images.fitnessmagazine.mdpcdn.com/sites/story/with_multydots.65560759.jpg, image name: with_multydots.65560759.jpg 
Url: //images.fitnessmagazine.mdpcdn.com/sites/story/image.png, image name: image.png 
Url: //images.fitnessmagazine.mdpcdn.com/sites/story/without_ext is not image url 

September 5, 2020 Category : PHP

Sometimes, we may require to get only filename or image name from full path or url in your PHP project or any framwwork project at that time you can get file name using php pre-define function basename.

If you ever use this function then you can see as bellow example how it's works. I have one image url like, i want to just get image name then you can get image name like this way :

Example:

basename["//itsolutionstuff.com/upload/Laravel-5-Mailgun.png"];

Hardik Savani

I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.

Follow Me:

We are Recommending you

    [PHP 4, PHP 5, PHP 7, PHP 8]

    basenameReturns trailing name component of path

    Description

    basename[string $path, string $suffix = ""]: string

    Note:

    basename[] operates naively on the input string, and is not aware of the actual filesystem, or path components such as "..".

    Caution

    basename[] is locale aware, so for it to see the correct basename with multibyte character paths, the matching locale must be set using the setlocale[] function. If path contains characters which are invalid for the current locale, the behavior of basename[] is undefined.

    Parameters

    path

    A path.

    On Windows, both slash [/] and backslash [\] are used as directory separator character. In other environments, it is the forward slash [/].

    suffix

    If the name component ends in suffix this will also be cut off.

    Return Values

    Returns the base name of the given path.

    Examples

    Example #1 basename[] example

    The above example will output:

    1] sudoers
    2] sudoers.d
    3] passwd
    4] etc
    5] .
    6] 
    

    See Also

    • dirname[] - Returns a parent directory's path
    • pathinfo[] - Returns information about a file path

    Anonymous

    5 years ago

    It's a shame, that for a 20 years of development we don't have mb_basename[] yet!

    // works both in windows and unix
    function mb_basename[$path] {
        if [preg_match['@^.*[\\\\/][[^\\\\/]+]$@s', $path, $matches]] {
            return $matches[1];
        } else if [preg_match['@^[[^\\\\/]+]$@s', $path, $matches]] {
            return $matches[1];
        }
        return '';
    }

    stephane dot fidanza at gmail dot com

    15 years ago

    Support of the $suffix parameter has changed between PHP4 and PHP5:
    in PHP4, $suffix is removed first, and then the core basename is applied.
    conversely, in PHP5, $suffix is removed AFTER applying core basename.

    Example:


    Result in PHP4: file
    Result in PHP5: Texture]

    swedish boy

    12 years ago

    Here is a quick way of fetching only the filename [without extension] regardless of what suffix the file has.

    zandor_zz at yahoo dot it

    14 years ago

    It might be useful to have a version of the function basename working with arrays too.

    lazy lester

    16 years ago

    If your path has a query string appended, and if the query string contains a "/" character, then the suggestions for extracting the filename offered below don't work.

    For instance if the path is like this:
    //www.ex.com/getdat.php?dep=n/a&title=boss

    Then both the php basename[] function, and also
    the $_SERVER[QUERY_STRING] variables get confused.

    In such a case, use:

    pvollma at pcvsoftware dot net

    17 years ago

    There is a real problem when using this function on *nix servers, since it does not handle Windows paths [using the \ as a separator]. Why would this be an issue on *nix servers? What if you need to handle file uploads from MS IE? In fact, the manual section "Handling file uploads" uses basename[] in an example, but this will NOT extract the file name from a Windows path such as C:\My Documents\My Name\filename.ext. After much frustrated coding, here is how I handled it [might not be the best, but it works]:

    Chủ Đề