PHP File and Directory Functions

Home > Advance PHP Tutorial > PHP File and Directory Functions



Common PHP File and Directory Functions

PHP Comes with a whole range of file and directory manipulation functions.

These Predefined Function allow you to check file attributes like copy, move delete etc.

Sr No Function What it Does
1 file_exists() Tests if a file or directory exists or not. This function work for file and Directory Both
2 filesize() Returns the size of a file in bytes
3 realpath() Returns the absolute path of a file
4 pathinfo() Returns an array of information about a file and its path
5 stat() Provides information on file attributes and its permissions
6 is_readable() Tests if a file is readable
7 is_writable() Tests if a file is writable
8 is_executable Tests if a file is executable
9 fopen() Open a file(Write mode of file why want to open)
10 fread() Read a file(Files must be opened in read mode)
11 fgets() Read first line of a file(File must be opened in read mode)
12 fgetc() Read first Character of a file(File must be opened in read mode)
13 fwrite() Write Contents/information inside a file(File must be opened in write mode)
14 file_put_contents() Write Contents/information inside a file(it accept 2 parameters, file name and content)
15 file_get_contents() Read information from a file(it accpet 1 parameters file name only)
16 is_file() Tests if a directory is a file
17 copy() Copies a file
18 rename() Rename a file
19 unlink() Deletes a file
20 include() Reads an external file into the current PHP Script
21 require() Reads an external file into the current PHP Script
22 include_once() Reads an external file into the current PHP Script(One time only)
23 require_once() Reads an external file into the current PHP Script(One time only)
24 fclose() Close a open file
25 is_dir() Tests if a directory entry is a directory
26 is_dir() Tests if a directory entry is a directory
27 mkdir() Creates a directory
28 rmdir() Removes a directory
29 opendir() Open a directory
30 readdir() Read a directory(It reads a signle file at a time, so must use loop to read entire directory)
31 scandir() Read a directory(Return result in array format)