How to remove the base path from a file path in PHP

Removing the file path from a filename in PHP is be easy!

The basename() function will remove the file’s path from your string and leave only the filename. So, for example, it means that if your file path string is: ‘/home/anto.online/apac/basepath/test.php’, then using basename($path) will return only the filename. The result will thus be: ‘test.php’.

Functions used in conjunction

The basename() function is often used with these PHP functions:

realpath() – Returns the full absolute path, including the filename.

dirname() – Can return the file path of a parent directory. If there are no slashes in the file path, a dot (‘.‘) is returned, indicating the current file path. Otherwise, the returned string is a file path with any trailing component removed.

Example of how to use these functions in PHP:

//get the absolute path of a file
$real = realpath('./test.php');
echo 'realpath result: ' . $real . '<br>';
//output: /home/anto.online/apac/basepath/test.php

//get the absolute path of a file
$dir = dirname('./test.php');
echo 'dirname result: ' . $dir . '<br>';
//output: .

//get the filename and remove the filepath
$base = basename($real);
echo 'basename result: ' . $base . '<br>';
//output: test.php

Practical example – Removing file path from error message

Let’s say you need to write an error handler in PHP. The custom handler must not expose paths to the front-end. Your remove file paths for security reasons.

We can use basepath() to sanitize the $file variable and remove file paths.

Thus, for the custom error handler example below:

function customErrorHandler($code, $message, $file, $line) {
$logger = createLogger();
...

We can do this by determining the string position of the ‘/’ and then use a  substring function. (This would be the hard way!)

Alternatively, we can use the basename() method in PHP. It even considers the differences between Windows and Linux!

Adding the basename() function will make our error handler look like this:

function customErrorHandler($code, $message, $file, $line) {
$logger = createLogger();
...
$message = 'Error ID: [' . $uid . '] [' . SITE_ID . '] ' . $user . 'Message: [' . $message . '] At: [' . basename($file) . '] Line: [' . $line . '] Code: [' . $code . ']';
...

You may also be interested in



About the Authors

Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee



About Anto Online

Having started his career in 1999 as a Desktop Support Engineer, Anto soon changed paths and became a developer. After several years of development experience, he transitioned into a consultant. As an enterprise application consultant for a leading SaaS software provider, Anto specializes in AWS's serverless technologies. By day, Anto focuses on helping customers leverage the power of serverless technologies. By night, he indulges his passion for cloud computing by playing with Python and trying out things that are currently beyond the scope of his work. Sometimes Anto needs help as there are not enough hours at night. So Anto relies on a team of fellow Cloud enthusiasts to help him out. Each one is a Cloud expert in their own right, and Anto takes great pride in helping them learn and grow.

View all posts by Anto Online →

7 Comments on “How to remove the base path from a file path in PHP”

  1. Awesome blog for knowledge. Thank you for sharing this useful article. This blog is a very helpful to me. Keep sharing this type informative articles with us.

  2. Awesome blog for knowledge. Thank you for sharing this useful article. This blog is a very helpful to me. Keep sharing this type informative articles with us.

  3. The best explanation I have read and very easy to understand and apply. Thank you for sharing.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.