PHP | filetype( ) Function

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

The filetype() function accepts the filename as a parameter and returns one of the seven file types on success and False on failure.

The seven possible return values of the filetype() function are:



  • file: regular file
  • dir: directory
  • char: character special device
  • link: symbolic link
  • fifo: FIFO (named pipe)
  • block: block special device
  • unknown: unknown file type

The result of the filetype() function is cached and a function called clearstatcache() is used to clear the cache.

Syntax:

filetype( $filename )

Parameters: The filetype() function in PHP accepts only one parameter $filename. It specifies the filename of the file whose type you want to know.

Return Value: It returns the type of a file on success and False on failure.

Errors And Exception:

  1. For files which are larger than 2GB some filesystem functions may return unexpected results since PHP’s integer type is signed and many platforms use 32bit integers.
  2. The filetype() function emits an E_WARNING in case of a failure.
  3. The buffer must be cleared if the filetype() function is used multiple times.
  4. The filetype() function emits an E_NOTICE message if the stat call fails or if the file type is unknown.

Examples:

Input : filetype("gfg.txt");
Output : file

Input : filetype("documents");
Output : dir

Below programs illustrate the filetype() function.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// displaying file type using
// filetype() function
echo filetype("gfg.txt");
  
?>

chevron_right


Output:

file

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// displaying file type using
// filetype() function
$myfile = "documents";
  
echo $myfile . ': ' . filetype($myfile);
  
?>

chevron_right


Output:

documents : dir

Reference:
http://php.net/manual/en/function.filetype.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.