44

Say I have an path: images/alphabet/abc/23345.jpg

How do I remove the file at the end from the path? So I end up with: images/aphabet/abc/

4 Answers 4

72

You want dirname()

4
  • 4
    What if the path is "images/alphabet/abc/"?
    – zPuls3
    Oct 24, 2014 at 9:52
  • 1
    @zPuls3 "Given a string containing the path of a file or directory, this function will return the parent directory's path." Oct 29, 2014 at 23:17
  • 1
    I disagree from this. Remove filename from path !== parent folder name. @abney317 should be the right answer
    – Machado
    Oct 3, 2016 at 17:30
  • dirname() only gives the parent folder, it will fail w/o a filename in the end of path.
    – Machado
    Oct 18, 2016 at 13:21
28

dirname() only gives you the parent folder's name, so dirname() will fail where pathinfo() will not.

For that, you should use pathinfo():

$dirname = pathinfo('images/alphabet/abc/23345.jpg', PATHINFO_DIRNAME);

The PATHINFO_DIRNAME tells pathinfo to directly return the dirname.

See some examples:

  • For path images/alphabet/abc/23345.jpg, both works:

    <?php
    
    $dirname = dirname('images/alphabet/abc/23345.jpg'); 
    // $dirname === 'images/alphabet/abc/'
    
    $dirname = pathinfo('images/alphabet/abc/23345.jpg', PATHINFO_DIRNAME); 
    // $dirname === 'images/alphabet/abc/'
    
  • For path images/alphabet/abc/, where dirname fails:

    <?php
    
    $dirname = dirname('images/alphabet/abc/'); 
    // $dirname === 'images/alphabet/'
    
    $dirname = pathinfo('images/alphabet/abc/', PATHINFO_DIRNAME); 
    // $dirname === 'images/alphabet/abc/'
    
2
  • Did you test your examples? dirname() and pathinfo() always return the path without ending slash. And at least in PHP 8.0 dirname() and pathinfo() gives you identical results. For 'images/alphabet/abc/' both functions returns "images/alphabet". So currently there's no difference between them. Apr 21, 2022 at 15:20
  • @PaulMelekhov please see the answer date, obviously it wasn't in PHP 8.0. Now if you ask me it's probably tested in PHP 5.6 or 5.4 and those comments were written by hand, they're intended to exemplify the answer and not to be some output replica, so you can ignore the trailing slash or suggest an edit to the answer.
    – Machado
    Apr 22, 2022 at 20:58
19
<?php
    $path = pathinfo('images/alphabet/abc/23345.jpg');
    echo $path['dirname'];
?>

http://php.net/manual/en/function.pathinfo.php

0

Note that when a string contains only a filename without a path (e.g. "test.txt"), the dirname() and pathinfo() functions return a single dot (".") as a directory, instead of an empty string. And if your string ends with "/", i.e. when a string contains only path without filename, these functions ignore this ending slash and return you a parent directory. In some cases this may be undesirable behavior and you need to use something else. For example, if your path may contain only forward slashes "/", i.e. only one variant (not both slash "/" and backslash "\") then you can use this function:

function stripFileName(string $path): string
{
    if (($pos = strrpos($path, '/')) !== false) {
        return substr($path, 0, $pos);
    } else {
        return '';
    }
}

Or the same thing little shorter, but less clear:

function stripFileName(string $path): string
{
    return substr($path, 0, (int) strrpos($path, '/'));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.