Thursday, November 27, 2014

How to Check File Extension Quickly in PHP

Want to check a file extension in PHP very quickly?? Then this right place to get deep information about it. Maybe, many times you need to know about the extension in PHP. Refer the below function, you will find, how to do it with using less small code.  

Check File Extension Quickly in PHP


Check the below PHP  Code to get is very quickly :-

<?php

$filename = "/home/darian/any.file.name.jpeg";

// get the php file type
$extension = pathinfo($filename, PATHINFO_EXTENSION);

// display the extension: This will echo out ".jpeg"
echo $extension;

?>

How above code works :-

We use the php function pathinfo() which will take a path (such as “/home/darian/any.file.name.jpeg” in our example) and will return the information about that full path. For example, if we run the following code:

$path_info = pathinfo($filename);

It would output an array with information similar to this below

Array
(
    [dirname] => /home/darian
    [basename] => any.file.name.jpeg
    [extension] => jpeg
    [filename] => any.file.name
)

In above code, the element extension contains the file extension. Now you can speed this function up even further by adding the flag PATHINFO_EXTENSION which tells the function to only return the file extension as a string rather than an array with all the additional information.

0 comments:

Post a Comment