Return to Snippet

Revision: 43927
at April 3, 2011 16:53 by donkeykong


Updated Code
/*
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. 
*/

Revision: 43926
at April 3, 2011 16:46 by donkeykong


Updated Code
/*
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. 
*/

Revision: 43925
at April 3, 2011 16:43 by donkeykong


Initial Code
/*
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. 
*/

Initial URL


Initial Description
A flexible way to get the file name without the extension

Initial Title
Get the  file name without the extension from a file\'s full name

Initial Tags
file, extension

Initial Language
PHP