What is the filetype() Function?

The filetype() function is a built-in PHP function that returns the type of a file. This function returns the file type as a string.

Here's the basic syntax of the filetype() function:


Where filename is the name of the file to be checked.

How to Use the filetype() Function?

Using the filetype() function is straightforward. Here are the steps to follow:

  1. Call the filetype() function, passing in the name of the file you want to check.
  2. The function will return the type of the file as a string.

Here's an example code snippet that demonstrates how to use the filetype() function:


$filename = 'myfile.txt';
$filetype = filetype($filename);
echo "The file $filename is of type $filetype";

In this example, we check the type of the file myfile.txt using the filetype() function. We store the file type in the $filetype variable and output a message indicating the type of the file.


The filetype() function is a useful tool in PHP for checking the type of a file. By following the steps outlined in this guide, you can easily use the filetype() function in your PHP projects to check the type of files.

Practice Your Knowledge

What does the is_file() function in PHP do?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?