Click to See Complete Forum and Search --> : printing the filename with out extension


roshanjameer
03-21-2007, 01:43 PM
hi,


i want to print the file name with out extension.here is my code for getting the extension.but how i can get only file name without extension

<?php
$file = "welcome.txt";
$aFile = explode(".", $file);
$ext = array_pop($aFile);
echo $ext;
?>

any of your idea will be appreciated.

thanks
mrjameer.

JoeHoldcroft
03-21-2007, 02:11 PM
That code does remove the file extension.. Do you mean print it with the extension? To do that, just echo the filename without the code to remove everything past the period:

<?php
$file = "welcome.txt";
echo $file;
?>

NogDog
03-21-2007, 02:52 PM
function fileNameParts($file)
{
$parts = explode('.', basename($file));
$result['ext'] = (count($parts) > 1) ? array_pop($parts) : '';
$result['base'] = implode('.', $parts);
return($result);
}
// usage:
$parts = fileNameParts('example.file_name.txt');
echo "file name without extension: " . $parts['base'];
echo "<br>file name extension: " . $parts['ext'];