Unknown file extension in PHP

I understand that using the PHP basename() function you can remove a known file extension from such a path,

 basename('path/to/file.php','.php') 

but what if you did not know what extension the file has or the length of this extension? How to do it?

Thanks in advance!

+6
source share
5 answers

pathinfo() has already been mentioned here, but I would like to add that from PHP 5.2 it also has an easy way to access the file name WITHOUT the extension.

 $filename = pathinfo('path/to/file.php', PATHINFO_FILENAME); 

The value of $filename will be file .

+9
source

You can extract the extension using pathinfo and disable it.

 // $filepath = '/path/to/some/file.txt'; $ext = pathinfo($filepath, PATHINFO_EXTENSION); $basename = basename($filepath, ".$ext"); 

Pay attention to . up to $ext

+4
source
 $filename = preg_replace('@\.([^\.]+) $@ ', '', $filename); 
0
source

Try the following: -

 $path = 'path/to/file.php'; $pathParts = pathinfo( $path ); $pathWihoutExt = $pathParts['dirname'] . DIRECTORY_SEPARATOR . $pathParts['filename']; 
0
source

You may try:

 $filepath = 'path/to/file.extension'; $extension = strtolower(substr(strrchr($filepath, '.'), 1)); 
0
source

Source: https://habr.com/ru/post/889632/


All Articles