Php basename without extension

Remove extension from a filename with PHP

If you’ve got a filename that you need to remove the extension from with PHP, there are a number of ways to do it. Here’s three ways, with some benchmarking.

Using pathinfo

The pathinfo() function returns an array containing the directory name, basename, extension and filename. Alternatively, you can pass it one of the PATHINFO_ constants and just return that part of the full filename:

$filename = 'filename.html'; $without_extension = pathinfo($filename, PATHINFO_FILENAME);

If the filename contains a full path, then only the filename without the extension is returned.

Using basename

If the extension is known and is the same for the all the filenames, you can pass the second optional parameter to basename() to tell it to strip that extension from the filename:

$filename = 'filename.html'; $without_extension = basename($filename, '.html');

If the filename contains a full path, then only the filename without the extension is returned.

Читайте также:  Gui application development in python

Using substr and strrpos

$filename = 'filename.html'; $without_extension = substr($filename, 0, strrpos($filename, "."));

If the filename contains a full path, then the full path and filename without the extension is returned. You could basename() it as well to get rid of the path if needed (e.g. basename(substr($filename, 0, strrpos($filename, «.»)))) although it’s slower than using pathinfo.

Benchmarking

Running each of these in a loop 10,000,000 times on my Mac with PHP 5.4:

pathinfo: 10.13 seconds
basename: 7.87 seconds
substr/strrpos: 6.05 seconds
basename(substr/strrpos): 11.98 seconds

If the filename doesn’t contain the full path or it doesn’t matter if it does, then the substr/strrpos option appears to be the fastest.

If the filename does contain a path and you don’t want the path but do know what the extension you want to remove is, then basename appears to be the fastest.

If the filename contains a path, you don’t want the path and you don’t know what the extension is, then use the pathinfo() option.

Conclusion

There will be plenty of other ways to do this, and some may be faster. In a lot of cases, the speed probably doesn’t really matter that much (the 10 seconds to run pathinfo was 10 million times, after all); the purpose of this post was to show a few ways to remove the extension from the filename with PHP.

Источник

Use of basename() in PHP

The basename() function is a built-in function of PHP that retrieves the filename from a given path. It can be used to print only the name of the file from a filename or file path. This function can also be used to print the existing script name. The main purpose of this function is to find out the filename or current script name for any programming purposes. How the basename() function can be used in PHP is shown in this tutorial.

Syntax:
string basename (string $path [, string $suffix ])

This function can take two arguments. The first argument is mandatory and will take filename or filename with the path as a string value. The second argument is optional and is used to get only the filename without extension.

Example1: Read filename from the existing and non-existing filename

The following example shows the use of the basename() function without the optional argument.

Create a PHP file with the following script. Here, the basename() function is used for the existing and non-existing files. Check() function is defined to check if the particular file exists or not. Both hello.txt and world.txt files are used in the basename() function to find out the filename with the extension.

function Check ( $file )
{
if ( file_exists ( $file ) )
echo » $file exists.
» ;
else
echo » $file does not exist.
» ;
}

//Set the filename that exists
$basepath1 = «hello.txt» ;

//Use of basename() function without optional parameter
echo «

The filename with extension is » . basename ( $basepath1 ) . «

» ;

//Set the filename that does not exist
$basepath2 = «world.txt» ;

//Use of basename() function without optional parameter
echo «

The filename with extension is » . basename ( $basepath2 ) . «

» ;

//Use of basename() function with optional parameter
echo «

The filename without extension is » . basename ( $basepath1 , «.txt» ) . «

» ;

Output:
The following output will appear after running the above script from the server. The output shows that the hello.txt file exists in the current location, and the basename() function returns the filename. The world.txt file does not exist in the current location, but the basename() function still returns the filename for this file. Thus, the basename() function returns the filename from a file path whether the file exists or not.

Example2: Read filename from the file path

In the previous example, only the filename is passed in the first argument of the basename() function. This example shows the use of the basename() function to find out the filename with an extension and without an extension from the file path. “.php” is used as the optional argument value of the basename() function. If the PHP file exists in the file path, then the basename() function will return the filename without an extension from the path.

//Set the filepath
$filepath = «var/www/html/php/book.php» ;

//Retrieve the filename with extension
echo «The name of the file with extension is » ;
echo basename ( $filepath ) . «
» ;

//Retrieve the filename without extension
echo «The name of the file without extension is » ;
echo basename ( $filepath , «.php» ) . «
» ;

Output:
The following output will appear after running the above script from the server. The path that is used in the script, ‘/var/www/html/php/book.php‘, contains a PHP file, and the basename() function returns book.php when used without an optional argument and returns book when it is used with an optional argument.

Example3: Read filename from URL address with query

The following example shows how the basename() function can be used to retrieve the filename from a URL address that contains query variables.

Create a PHP file with the following script. The explode() function is used here to separate the URL and the query string. This function returns an array. The first element of the array contains the URL, and the second element of the array contains the query string value. The basename() function is used to find out the filename from the first element of the array.

//Set the URL address with query parameter
$url = «http://localhost/php/customer.php?id=108967» ;

//Retrieve the filepath from the URL
$filepath = explode ( «?» , $url ) ;

//Retrieve the filename with extension
echo «The name of the file with extension is » ;
echo basename ( $filepath [ 0 ] ) . «
» ;

Output:
The following output will appear after running the above script from the server. Here, the filename is customer.php.

Example4: Read the directory and the directory after omitting the last directory from the path

The basename() function can also be used to find out the directory name from a path. It is used in the following example to find out the current directory name and the directory name before the current directory from the path.

Create a PHP file with the following script. The $_SERVER[‘PHP_SELF’] is used in the dirname() function to read the full path of the current script, and the basename() function is used to read the directory name that contains this script. When a particular path is defined in the dirname() function, and ‘/’ is used in the second argument of this function, then the path will read the directory path by omitting the last directory name. In this case, the basename() function will return the directory name after omitting the last directory from the path.

//Read the current directory
$current_dir = basename ( dirname ( $_SERVER [ ‘PHP_SELF’ ] ) , «/» ) ;

//Print the current directory
echo «The current working directory is : » . $current_dir . «
» ;

//Read the parent directory of the path
$dir = basename ( dirname ( ‘/var/www/html/php’ ) , «/» ) ;

//Print the parent directory name of the path
echo «The previous directory of the given path is : » . $dir . «» ;
?>

Output:
The following output will appear after running the above script from the server.

Example5: Read the current script name

The basename() function can also be used to read the current script name. When __FILE__ is used in the first argument of the basename() function, it will return the script filename as output.

//Read the current script name
echo «The name of the current script is : » . basename ( __FILE__ ) . «» ;

Output:
The following output will appear after running the above script from the server. The output shows the executing script file name.

Conclusion

The basename() function is a useful function of PHP when the coder works with a file or directory for various purposes. Different uses of the basename() function are explained in this tutorial using simple examples to help the readers understand its proper use and apply it in their PHP script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

PHP basename() Function

The basename() function returns the filename from a path.

Syntax

Parameter Values

Parameter Description
path Required. Specifies a file path
suffix Optional. A file extension. If the filename has this file extension, the file extension will be cut off

Technical Details

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

basename( ) function in PHP

The basename() function gets the the filename component of a path. The file path is set as a parameter.

Syntax

Parameters

  • file_path − Set the path of file or directory to be checked. Required.
  • suffix − Set the extension of the file. Optional.

Return

The basename() function returns the basename of the file.

Example

The following is an example that checks for file “new.php” and returns the basename with the extension since we added the suffix parameter.

Output

Example

Let us see another example that checks the file and display the basename without the extension.

Output

karthikeya Boyini

I love programming (: That’s all I know

  • Related Articles
  • Is there PHP basename() equivalent in MySQL?
  • filter_has_var() function in PHP
  • filter_id() function in PHP
  • filter_input() function in PHP
  • filter_input_array() function in PHP
  • filter_list() function in PHP
  • filter_var_array() function in PHP
  • filter_var() function in PHP
  • constant() function in PHP
  • define() function in PHP
  • defined() function in PHP
  • die() function in PHP
  • eval() function in PHP
  • exit() function in PHP
  • get_browser() function in PHP

Источник

Оцените статью