Get file extension on PHP

There are many ways to retrieve files extension with PHP, but we will have to choose one that feets our needs and gives more reliable results.
When dealing with file extension you should know that a file extension is not always 3 characters long (ex:  foto.jpeg, index.html) and that it doesn’t start after the last dot (.) in the file name (ex: program.tar.gz,

Let’s write the filename_details function that we will use to retrieve the extension of the file or any file you have:

function filename_details($filename, $dots){
  $filename_no_ext = $filename;
    $temp_arr = explode('.', $filename_no_ext);
    $extension = '.'.array_pop($temp_arr).$extension;
    $filename_no_ext = implode('.', $temp_arr);
  $extension = substr($extension, 1);
  return array($filename_no_ext, $extension);

  [0] => module
  [1] =>


Leave a comment

Your email address will not be published.