PHP Filesystem Functions

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().
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():
$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 functionsSplit, clean up, and match file paths. These work on the path itself and mostly do not touch the disk.
| Function | Description | Tutorial |
|---|---|---|
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 functionsFind out how much space a disk or partition has and how much of it is free.
| Function | Description | Tutorial |
|---|---|---|
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 functionsOpen files, read and write them step by step or in one call, move the file pointer, and close the stream.
| Function | Description | Tutorial |
|---|---|---|
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 functionsRead and write comma-separated values one row at a time.
File info & permissions
20 functionsRead size, type, owner, and timestamps, and change permissions, ownership, and modification times.
| Function | Description | Tutorial |
|---|---|---|
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 functionsTest whether a path exists and what kind of item it is, or whether the script may read, write, or run it.
| Function | Description | Tutorial |
|---|---|---|
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 functionsVerify and store files sent by HTTP POST uploads.
| Function | Description | Tutorial |
|---|---|---|
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 |
Links
4 functionsCreate hard and symbolic links and read information about them.
| Function | Description | Tutorial |
|---|---|---|
link() | Create a hard link. | Try it |
symlink() | Create a symbolic link. | Try it |
readlink() | Return the target of a symbolic link. | Try it |
linkinfo() | Return information about a link. | Try it |
Directories
3 functionsCreate and remove folders and find files that match a pattern.
Misc
8 functionsCopy, rename, and delete files, create temporary files, and parse INI configuration.
| Function | Description | Tutorial |
|---|---|---|
copy() | Copy a file. | Try it |
rename() | Rename or move a file or folder. | Try it |
unlink() | Delete a file. | Try it |
delete | Manual 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
Start with basename()
Open the first guide—syntax, examples, and FAQs.
6 people found this page helpful
