How to Get a File Extension in PHP

  1. Use pathinfo() Function to Get File Extension in PHP
  2. Use SplFileInfo() Construct and getExtension() Function to Get File Extension in PHP

In this article, we will introduce methods to get a file extension in PHP.

  • Using pathinfo() function
  • Using SplFileInfo construct and getExtension function

Use pathinfo() Function to Get File Extension in PHP

We will use the built-in function pathinfo() to get the file extension. This function extracts the path information from the given path. The correct syntax to use this function is as follows.

pathinfo($pathName, $options);

The built-in function pathinfo() has two parameters. The details of its parameters are as follows

Parameters Description
$pathName mandatory It is the string containing the path with file name and extension. We will extract path info from this string.
$options optional This parameter specifies the path elements. For example, if we want to find a file name only, we can pass PATHINFO_FILENAME as an option. The other options are PATHINFO_DIRNAME, PATHINFO_BASENAME, and PATHINFO_EXTENSION.

This function returns an associative array containing a directory name, base name, extension, and file name. If the $option parameter is passed, it returns a string.

The program below shows can we use the pathinfo() function to get file extension.

<?php 
$path = "E:\work\CM\myppt.ppt";
$extension = pathinfo($path, PATHINFO_EXTENSION);
echo("The extension is $extension.");  
?> 

We have passed the $option parameter. The function has returned a string containing file extension.

Output:

The extension is ppt. 

If we don’t pass the $option parameter, the function will return an associative array.

<?php 
$path = "E:\work\CM\myppt.ppt";
$array = pathinfo($path);
echo("The associative array is: \n");  
var_dump($array);
?> 

Output:

The associative array is: 
array(4) {
  ["dirname"]=>
  string(1) "."
  ["basename"]=>
  string(20) "E:\work\CM\myppt.ppt"
  ["extension"]=>
  string(3) "ppt"
  ["filename"]=>
  string(16) "E:\work\CM\myppt"
}

Use SplFileInfo() Construct and getExtension() Function to Get File Extension in PHP

In PHP, we can also use SplFileInfo() construct to get the file extension. This construct will create a new SplFileInfo object. After that we can use getExtension() function to get the file extension. The correct syntax to use this construct is as follows:

$variableName = new SplFileInfo($pathName);

The construct SplFileInfo() accepts one parameter. The detail of its parameter is as follows

Parameters Description
$pathName mandatory It is the string that contains our file’s path. We will use this string to extract file extension.

We will use getExtension() function to get the file extension. The correct syntax to use this function is as follows:

getExtension(void);

This function accepts no parameters. It returns file extension.

The program that gets the file extension is as follows:

<?php 
$path = "E:\work\CM\myppt.ppt";
$file = new SplFileInfo($path);
$extension  = $file->getExtension();
echo("The extension is: $extension.");  
?> 

Output:

The extension is: ppt. 

Related Article - PHP File

  • How to Read a Large File Line by Line in PHP
  • How to Write Into a File in PHP
  • comments powered by Disqus