Get extension of a file using PHP

PHP

Read {count} times since 2020

This function will allow you to get a file’s extension in PHP.
function getExtension($fileName){
   $i = strrpos($fileName, ".");
   if (!$i) {
      return "";
   }
   $length = strlen($str) - $i;
   $extens = substr($str, $i+1, $length);
   return $extens;
}

For example you need to get a extension of the file photo.jpg. To get the extension type this code in your PHP file.

<span style="color: #660000;"><?php</span> <span style="color: #660000;">echo</span> getExtension<span style="color: #660000;">(</span>'<b><span style="color: #e06666;">photo.jpg</span></b>'<span style="color: #660000;">);</span> <span style="color: #660000;">?></span>

The above code will print out "jpg".

Show Comments