Jump to content

Getting extension of a file using one line code


renjikk

Recommended Posts

  Quote

$s="file2.php";
$s2=substr($s,-3);
echo $s2;
[code]

 

this only works with an extension of that is 3 chars long...

how about .jpeg, .html etc. etc.

 

to be sure, look for the last "." in the file name with strrpos

<?php
$s = "myfile.extension";
$dotPos = strrpos($s, ".");
$fileExtension = substr($s, $dotPos+1);
$fileName = substr($s, 0, $dotPos);
?>

 

  • 1 month later...

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.