PHP Filesystem Functions

By | September 29, 2022

PHP Filesystem Introduction

The filesystem functions allow you to access and manipulate the filesystem.


Installation

The filesystem functions are part of the PHP core. There is no installation needed to use these functions.


Runtime Configuration

The behavior of the filesystem functions is affected by settings in php.ini.

Filesystem configuration options:

NameDefaultDescriptionChangeable
allow_url_fopen“1”Allows fopen()-type functions to work with URLs (available since PHP 4.0.4)PHP_INI_SYSTEM
user_agentNULLDefines the user agent for PHP to send (available since PHP 4.3)PHP_INI_ALL
default_socket_timeout“60”Sets the default timeout, in seconds, for socket based streams (available since PHP 4.3)PHP_INI_ALL
from“”Defines the anonymous FTP password (your email address)PHP_INI_ALL
auto_detect_line_endings“0”When set to “1”, PHP will examine the data read by fgets() and file() to see if it is using Unix, MS-Dos or Mac line-ending characters (available since PHP 4.3)PHP_INI_ALL

Unix / Windows Compatibility

When specifying a path on Unix platforms, the forward slash (/) is used as directory separator. However, on Windows platforms, both forward slash (/) and backslash (\) can be used.

PHP Filesystem Functions

PHP: indicates the earliest version of PHP that supports the function.

FunctionDescriptionPHP
basename()Returns the filename component of a path3
chgrp()Changes the file group3
chmod()Changes the file mode3
chown()Changes the file owner3
clearstatcache()Clears the file status cache3
copy()Copies a file3
delete()See unlink() or unset() 
dirname()Returns the directory name component of a path3
disk_free_space()Returns the free space of a directory4
disk_total_space()Returns the total size of a directory4
diskfreespace()Alias of disk_free_space()3
fclose()Closes an open file3
feof()Tests for end-of-file on an open file3
fflush()Flushes buffered output to an open file4
fgetc()Returns a character from an open file3
fgetcsv()Parses a line from an open file, checking for CSV fields3
fgets()Returns a line from an open file3
fgetss()Returns a line, with HTML and PHP tags removed, from an open file3
file()Reads a file into an array3
file_exists()Checks whether or not a file or directory exists3
file_get_contents()Reads a file into a string4
file_put_contentsWrites a string to a file5
fileatime()Returns the last access time of a file3
filectime()Returns the last change time of a file3
filegroup()Returns the group ID of a file3
fileinode()Returns the inode number of a file3
filemtime()Returns the last modification time of a file3
fileowner()Returns the user ID (owner) of a file3
fileperms()Returns the permissions of a file3
filesize()Returns the file size3
filetype()Returns the file type3
flock()Locks or releases a file3
fnmatch()Matches a filename or string against a specified pattern4
fopen()Opens a file or URL3
fpassthru()Reads from an open file, until EOF, and writes the result to the output buffer3
fputcsv()Formats a line as CSV and writes it to an open file5
fputs()Alias of fwrite()3
fread()Reads from an open file3
fscanf()Parses input from an open file according to a specified format4
fseek()Seeks in an open file3
fstat()Returns information about an open file4
ftell()Returns the current position in an open file3
ftruncate()Truncates an open file to a specified length4
fwrite()Writes to an open file3
glob()Returns an array of filenames / directories matching a specified pattern4
is_dir()Checks whether a file is a directory3
is_executable()Checks whether a file is executable3
is_file()Checks whether a file is a regular file3
is_link()Checks whether a file is a link3
is_readable()Checks whether a file is readable3
is_uploaded_file()Checks whether a file was uploaded via HTTP POST3
is_writable()Checks whether a file is writeable4
is_writeable()Alias of is_writable()3
link()Creates a hard link3
linkinfo()Returns information about a hard link3
lstat()Returns information about a file or symbolic link3
mkdir()Creates a directory3
move_uploaded_file()Moves an uploaded file to a new location4
parse_ini_file()Parses a configuration file4
pathinfo()Returns information about a file path4
pclose()Closes a pipe opened by popen()3
popen()Opens a pipe3
readfile()Reads a file and writes it to the output buffer3
readlink()Returns the target of a symbolic link3
realpath()Returns the absolute pathname4
rename()Renames a file or directory3
rewind()Rewinds a file pointer3
rmdir()Removes an empty directory3
set_file_buffer()Sets the buffer size of an open file3
stat()Returns information about a file3
symlink()Creates a symbolic link3
tempnam()Creates a unique temporary file3
tmpfile()Creates a unique temporary file3
touch()Sets access and modification time of a file3
umask()Changes file permissions for files3
unlink()Deletes a file3

PHP Filesystem Constants

PHP: indicates the earliest version of PHP that supports the constant.

ConstantDescriptionPHP
GLOB_BRACE  
GLOB_ONLYDIR  
GLOB_MARK  
GLOB_NOSORT  
GLOB_NOCHECK  
GLOB_NOESCAPE  
PATHINFO_DIRNAME  
PATHINFO_BASENAME  
PATHINFO_EXTENSION  
FILE_USE_INCLUDE_PATH  
FILE_APPEND  
FILE_IGNORE_NEW_LINES  
FILE_SKIP_EMPTY_LINES  
Category: PHP

Leave a Reply

Your email address will not be published. Required fields are marked *