Jump to content

Stripping off file extension from filename...


cgm225

Recommended Posts

If the filename never contain a dot, you can do like so:

 

<?php
$file = "movie.avi";
list($filename, $ext) = explode('.', $file);
// $filename = movie
// $ext = avi
?>

 

If it DOES contain dots, the above will fail. Instead you could do this:

 

<?php
$file = 'my.movie.avi';
$arr = explode('.', $file);
$ext = array_pop($arr);
$filename = implode('.', $arr);
// $filename = my.movie
// $ext = avi
?>

If you're using PHP v5.2 or greater, you can use the pathinfo() function with the PATHINFO_FILENAME constant:

<php
$filename = 'this.is.a.weird.file.name.longextension';
echo pathinfo($filename,PATHINFO_FILENAME);
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.