How To Use PHP Basename on Windows and Linux Servers

How to use php basename on windows and linux servers

When working with files within your Windows or Linux servers, there will be times when you will need to know the file name from a given path name, when working in PHP. Luckily, within PHP there are many inbuild functions available to programmers, which can be used, without the need to code your own function. PHP provides the function basename, but how does it work, and how can we use it within our code to ensure we're getting the correct filename out from a full system path when working with files and folders in PHP?

How does PHP basename work?

The PHP basename function works by taking a path (typically a full system path) which is provided as a string and will return the file name (aka. the base name) of the given path, which is returned as a string. PHP calls the base name the component of a path. Therefore, if you had a full system path for a file in the root of your directory, you may get something like this;

/var/www/vhosts/httpdocs/file.php

When run through `basename()` becomes;

file.php

You could use PHP's explode function to break out each forward slash, but that won't work on a Windows server. That's because Windows uses both slash and backslash. You also could use PHP's 'strrpos' function to find the last occurrence of the slash, but again, you'll fail if you ever moved your code from Windows to a Unix-based system (or vice versa). Instead, PHP's basename does all the hard work for you.

$filepath = '/var/www/vhosts/httpdocs/images/file.png';

# Outputs 'file.png'
echo basename($filepath);

It's worth noting that as basename is locale-aware it's important to ensure that by using the setlocale function to set your locale it matches your path names.

Why might basename be useful in coding?

The basename function becomes useful when you want to extract the filename from the full system path in order to do some operation on that particular file. Maybe you want to display this to a user. But you wouldn't want to show the full system directory path, as this is considered a security risk. It also might be useful if you want to extract the file extension from a file path. Let's say you had a folder full of PNGs that you wanted to display to a user, but didn't want to display the file extension to them. For that, you could use the second parameter of `basename`, a suffix. Suffixes can be used to cut off characters at the end of the filename.

$filepath = '/var/www/vhosts/httpdocs/images/file.png';

# Outputs 'file'
echo basename($filepath, '.png');

It does not only work with files it also works for folders. Because the basename function strips out all the slashes including the system path you are left with just the "base name", which in the below example will be the images folders. Therefore the output from the basename function will be "images".

$filepath = '/var/www/vhosts/httpdocs/images/';

# Outputs 'images'
echo basename($filepath);

This is particularly useful if, for example, you want to display the folder name to a user. If the code had used one of the nine different magic constants `__FILE__`, which displays the directory's full path and filename of a file, you may want to strip out the full path.

# Let's say for this example __FILE__ is '/var/www/vhosts/httpdocs/images_controller.php'

# Outputs 'images_controller'
echo basename(__FILE__, '.php');

When might basename not be useful?

There are times when using the full system path is better than stripping out the path name, to give you more protection. This might occur when deleting a file, or checking if a file exists in its location.

unlink - the function to delete a file, using the full system path here is safer than running without.

file_exists - checking if a file exists using PHP, here it's better to pass the full system path, to ensure you're checking in the exact location you'd planned in your code.

Conclusion

PHP is full of lots of pre-built functions, to make your job easier. Stripping out system paths to obtain just the filename is one you'll frequently perform. That's why it's great to make sure you understand how the basename function works, and how the suffix parameter might be useful to remove things such as file extensions. The basename function is a great example of a single function in PHP that can be used in many different ways and is what makes PHP an extremely powerful programming language.

  • Basename requires the path to be a string
  • Basename will return a string
  • The basename is not aware of path components such as double dot '..'
  • Make sure your locale, (via the `setlocale` function), matches path names (especially if you have multibyte character paths) to avoid issues with invalid characters in paths. That's because the basename is locale-aware

Senior PHP developer. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience, and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all