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:
- 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.
- The filetype() function emits an E_WARNING in case of a failure.
- The buffer must be cleared if the filetype() function is used multiple times.
- 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:
<?php
echo filetype ( "gfg.txt" );
?>
|
Output:
file
Program 2:
<?php
$myfile = "documents" ;
echo $myfile . ': ' . filetype ( $myfile );
?>
|
Output:
documents : dir
Reference:
http://php.net/manual/en/function.filetype.php
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
05 May, 2018
Like Article
Save Article