How to get the file type from URL in PHP?

❮ PHP Filesystem Reference

Example

Return the file type for "test.txt":

echo filetype("test.txt");
?>

Run Example »


Definition and Usage

The filetype() function returns the file type of a file.

Possible return values:

  • fifo
  • char
  • dir
  • block
  • link
  • file
  • socket
  • unknown

Note: The result of this function is cached. Use clearstatcache() to clear the cache.

Syntax

Parameter Values

ParameterDescriptionfilenameRequired. Specifies the file to check

Technical Details

Return Value:One of the file types on success, FALSE on failurePHP Version:4.0+

More Examples

Example

Return the file type for /images/:

echo filetype("/images/");
?>

Run Example »


❮ PHP Filesystem Reference

In PHP, there exist various ways of getting a file extension. In our tutorial, we have chosen the 5 best ways that will help you to get a file extension straightforwardly.

Go ahead and choose the one that matches your project better.

The first way is exploding a file variable and getting the last element of the array. That will be a file extension. Here is how you can do it:

In the framework of the second approach, we recommend detecting the last occurrence of ‘.’, which will return a ‘.jpg’. Afterwards, you need to remove the first character of a string using substr. As a result, the exact file extension ‘.jpg’ will be returned.

Here is an example:

The next way is to use strrpos for detecting the position of the last occurrence of ‘.’ inside a file name, incrementing that position by 1 so that it explodes a string from (.).

The example is shown below:

In the framework of this approach, you should apply a regular expression reach and replacement.

Here is an example:

And, the final approach that we recommend is to use the PHP pathinfo() function. It is aimed at returning information about the file. In case the second optional parameter is not passed, an associative array will be returned. It will include the dirname, extension, basename, and filename. Once the second parameter is passed, then specific information will be returned.

Here is an example:

So, after learning about all the possible ways described above, you are free to choose the best one for you. In our opinion, the simplest, yet the most efficient way among all these options is the pathinfo() function.

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

ParametersDescription
 
2mandatoryIt is the
 
3 containing the path with file name and extension. We will extract path info from this
 
3.
 
5optionalThis parameter specifies the path elements. For example, if we want to find a file name only, we can pass
 
6 as an option. The other options are
 
7,
 
8, and
 
9.

This function returns an associative array containing a directory name, base name, extension, and file name. If the

 
0 parameter is passed, it returns a string.

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

 

We have passed the

 
0 parameter. The function has returned a string containing file extension.

Output:

If we don’t pass the

 
0 parameter, the function will return an associative 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 4 Construct and 5 Function to Get File Extension in PHP

In PHP, we can also use

 
4 construct to get the file extension. This construct will create a new SplFileInfo object. After that we can use
 
5 function to get the file extension. The correct syntax to use this construct is as follows:

$variableName = new SplFileInfo($pathName);

The construct

 
4 accepts one parameter. The detail of its parameter is as follows

ParametersDescription
 
2mandatoryIt is the
 
3 that contains our file’s path. We will use this string to extract file extension.

We will use

 
5 function to get the file extension. The correct syntax to use this function is as follows:

How to know file type from URL in PHP?

index.php.
$filePath = "uploads/my_image. jpg";.
$fileInfo = pathinfo($filePath);.
$extension = $fileInfo['extension'];.
$filename = $fileInfo['filename'];.
print_r('File Name is "'. $filename. '" and extension is '. $extension);.
print_r($fileInfo);.

How to know the type of file in PHP?

The filetype() function in PHP is an inbuilt function which is used to return the file type of a specified file or a directory.

How to get the filename in PHP?

The current full-path filename is kept in system variable $_SERVER['PHP_SELF'] . You can use function basename() to get the filename without the path; or basename(path, suffix) to get the filename without the extension.

How to get file path extension in PHP?

Use pathinfo And, the final approach that we recommend is to use the PHP pathinfo() function. It is aimed at returning information about the file. In case the second optional parameter is not passed, an associative array will be returned. It will include the dirname, extension, basename, and filename.