PHP Filesystem Functions

Beginner
6 min read
Updated: Nov 2026
83 function guides
Function index

About This Index

This page is the PHP Filesystem Functions index for the tutorial track. Each row links to a full guide with syntax, examples, and FAQs.

Learn how paths, streams, and file checks fit together on /php/file-system. Use this page when you need a specific helper such as fopen(), file_get_contents(), or unlink().

Beginner path.

Start with basename(), file_get_contents(), file_put_contents(), and file_exists()—then learn fopen() with fgets() for large files.

How to Call a Filesystem Function

Filesystem helpers are global functions—pass a path, or a handle returned by fopen():

PHP
$file = __DIR__ . '/data.txt';

if (is_file($file)) {
    $text = file_get_contents($file);
}

$h = fopen($file, 'a');
if ($h !== false) {
    fwrite($h, "New line\n");
    fclose($h);
}

echo basename($file);   // data.txt

PHP Filesystem Functions Table

All function guides in this PHP tutorial series (83 tutorials in 10 categories).

Path helpers

7 functions

Split, clean up, and match file paths. These work on the path itself and mostly do not touch the disk.

FunctionDescriptionTutorial
basename()Return the file name part of a path.Try it
dirname()Return the parent directory of a path.Try it
pathinfo()Return the directory, file name, and extension of a path.Try it
realpath()Return the canonical absolute path of a file or folder.Try it
realpath_cache_get()Get the entries stored in the realpath cache.Try it
realpath_cache_size()Get the memory size used by the realpath cache.Try it
fnmatch()Check whether a name matches a shell wildcard pattern.Try it

Disk space

3 functions

Find out how much space a disk or partition has and how much of it is free.

FunctionDescriptionTutorial
disk_free_space()Return the free space, in bytes, of a directory's disk.Try it
disk_total_space()Return the total size, in bytes, of a directory's disk.Try it
diskfreespace()Alias of disk_free_space().Try it

Open, read & write streams

26 functions

Open files, read and write them step by step or in one call, move the file pointer, and close the stream.

FunctionDescriptionTutorial
fopen()Open a file or URL and return a handle.Try it
fclose()Close an open file handle.Try it
fread()Read up to a given number of bytes from a file handle.Try it
fwrite()Write a string to a file handle.Try it
fputs()Alias of fwrite().Try it
fgets()Read one line from a file handle.Try it
fgetc()Read a single character from a file handle.Try it
fgetss()Read a line and strip HTML tags (removed in PHP 8.0).Try it
fscanf()Read a line from a file and parse it by a format.Try it
feof()Check whether the file pointer is at the end of the file.Try it
fflush()Flush buffered output to the file.Try it
fsync()Force changes of a file, with its metadata, to disk.Try it
fdatasync()Force changes of a file's data to disk.Try it
fseek()Move the file pointer to a position.Try it
ftell()Return the current position of the file pointer.Try it
rewind()Move the file pointer back to the start.Try it
ftruncate()Cut an open file to a given length.Try it
flock()Lock or unlock a file for safe shared access.Try it
fpassthru()Output everything left on a file handle.Try it
set_file_buffer()Set the write buffer size of a stream (alias of stream_set_write_buffer()).Try it
file()Read a whole file into an array of lines.Try it
file_get_contents()Read a whole file into a string.Try it
file_put_contents()Write a string to a file.Try it
readfile()Read a file and write it to the output.Try it
popen()Open a pipe to a process.Try it
pclose()Close a pipe opened with popen().Try it

CSV

2 functions

Read and write comma-separated values one row at a time.

FunctionDescriptionTutorial
fgetcsv()Read a line from a file and parse it as CSV fields.Try it
fputcsv()Format an array as a CSV line and write it to a file.Try it

File info & permissions

20 functions

Read size, type, owner, and timestamps, and change permissions, ownership, and modification times.

FunctionDescriptionTutorial
stat()Return information about a file.Try it
lstat()Return information about a file or symbolic link.Try it
fstat()Return information about an open file handle.Try it
filesize()Return the size of a file in bytes.Try it
filetype()Return the type of a file, such as file, dir, or link.Try it
fileatime()Return the last access time of a file.Try it
filectime()Return the inode change time of a file.Try it
filemtime()Return the last modification time of a file.Try it
fileinode()Return the inode number of a file.Try it
filegroup()Return the group ID of a file.Try it
fileowner()Return the owner user ID of a file.Try it
fileperms()Return the permissions of a file.Try it
chmod()Change the permissions of a file.Try it
chown()Change the owner of a file.Try it
chgrp()Change the group of a file.Try it
lchown()Change the owner of a symbolic link.Try it
lchgrp()Change the group of a symbolic link.Try it
touch()Create a file or set its access and modification times.Try it
umask()Get or change the default permission mask for new files.Try it
clearstatcache()Clear the cached file status information.Try it

Existence checks

8 functions

Test whether a path exists and what kind of item it is, or whether the script may read, write, or run it.

FunctionDescriptionTutorial
file_exists()Check whether a file or folder exists.Try it
is_file()Check whether a path is a regular file.Try it
is_dir()Check whether a path is a directory.Try it
is_link()Check whether a path is a symbolic link.Try it
is_readable()Check whether a file can be read.Try it
is_writable()Check whether a file can be written to.Try it
is_writeable()Alias of is_writable().Try it
is_executable()Check whether a file can be executed.Try it

Uploads

2 functions

Verify and store files sent by HTTP POST uploads.

FunctionDescriptionTutorial
is_uploaded_file()Check whether a file was uploaded through HTTP POST.Try it
move_uploaded_file()Move an uploaded file to a new location.Try it

Directories

3 functions

Create and remove folders and find files that match a pattern.

FunctionDescriptionTutorial
mkdir()Create a directory.Try it
rmdir()Remove an empty directory.Try it
glob()Find file paths that match a pattern.Try it

Misc

8 functions

Copy, rename, and delete files, create temporary files, and parse INI configuration.

FunctionDescriptionTutorial
copy()Copy a file.Try it
rename()Rename or move a file or folder.Try it
unlink()Delete a file.Try it
deleteManual page for deleting files; use unlink().Try it
tempnam()Create a file with a unique name.Try it
tmpfile()Create a temporary file that is removed when closed.Try it
parse_ini_file()Parse an INI configuration file.Try it
parse_ini_string()Parse an INI configuration string.Try it

Frequently Asked Questions

PHP filesystem functions are the built-in functions for working with files and folders: splitting paths, opening and reading files, writing data, checking permissions and timestamps, creating or removing directories, and handling uploaded files. They need no extra library.
The Filesystem page teaches the ideas: paths, fopen() with fread() and fwrite(), file_get_contents() and file_put_contents(), existence checks, and unlink() with rename(). This index lists every function tutorial so you can open the exact one you need.
For small files use file_get_contents() and file_put_contents(). For large files or line-by-line work use fopen() with fgets() and fclose(). Before you read, check the path with file_exists() or is_file(), and build paths with __DIR__ so they do not depend on the working directory.
file_exists() returns true for any existing path, whether it is a file or a folder. is_file() is true only for a regular file, and is_dir() is true only for a directory. Use the specific one when the type matters.
The index covers every filesystem tutorial in the series. delete is a manual page that points to unlink(), fgetss() was removed in PHP 8.0 and is kept for older code, and diskfreespace() and is_writeable() are aliases of disk_free_space() and is_writable().
If you are new, read PHP Filesystem first. Then open fopen(), file_get_contents(), file_put_contents(), and file_exists() from the tables below.

Start with basename()

Open the first guide—syntax, examples, and FAQs.

basename() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful