/ Published in: PHP
A flexible way to get the file name without the extension
- Detailed notes are below the code.
- $str_full_filename = 'tricky_filename_.js.jpg.version2.jpg';
- $arr = explode( '.',$str_full_filename );
- $int_last_element = sizeof( $arr )-1;// the last element contains the extension
- $str_extension = $arr[ $int_last_element ];
- array_pop( $arr );// the filename (without the extension), doesn't need the last element
- $filename_without_extension = implode( '.',$arr );
- echo 'The extension is '.$str_extension;
- echo '<br /> The file name (without the extension) is: '.$filename_without_extension;
- This is a group of methods to get the extension of a file or get a filename without the extension; this is especially useful when you need to copy an image (say filename.jpg), resize and rename to something like the following:
- 1. filename_small.jpg
- 2. filename_medium.jpg
- 3. filename_large.jpg
- I'm not covering how to resize images, just a way to copy the name and the extension so you can keep names consistent.
- The way I did this is to anticipate strange file names, so files with an extension name in it more than once won't cause errors or inconsistency.
CommentsSubscribe to comments
You need to login to post a comment.