48

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

76

You want dirname()

Sign up to request clarification or add additional context in comments.

4 Comments

What if the path is "images/alphabet/abc/"?
@zPuls3 "Given a string containing the path of a file or directory, this function will return the parent directory's path."
I disagree from this. Remove filename from path !== parent folder name. @abney317 should be the right answer
dirname() only gives the parent folder, it will fail w/o a filename in the end of path.
32

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 Comments

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.
@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.
19
<?php
    $path = pathinfo('images/alphabet/abc/23345.jpg');
    echo $path['dirname'];
?>

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

Comments

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, '/'));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.