PHP 5 Filesystem Functions

Introduction to PHP Filesystem

Filesystem functions allow you to access and manipulate the file system.


Filesystem functions are part of the core of PHP. No installation is required to use these functions.

Runtime configuration

The behavior of the Filesystem function is affected by settings in php.ini.

Filesystem configuration options:

Name Default Description Changeable
allow_url_fopen "1" Allows fopen()-type functions to work with URLs (available since PHP 4.0.4) PHP_INI_SYSTEM
user_agent NULL Defines 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

PHP 5 Filesystem Functions

Function Description
basename () Returns the filename part of the path.
chgrp () Change the file group.
chmod () Change the file mode.
chown () Change the file owner.
clearstatcache () Clear the file status cache.
copy () Copy files.
delete () See unlink () or unset ()
dirname () Returns the directory name part of the path.
disk_free_space () Returns the free space of the directory.
disk_total_space () Returns the total disk capacity of a directory.
diskfreespace () Alias for disk_free_space ().
fclose () Close the open file.
feof () Tests whether the file pointer reaches the end of the file.
fflush() Flush buffered output to open files.
fgetc () Returns characters from an open file.
fgetcsv () Parse a line from the open file and verify CSV field .
fgets () Return a line from the open file.
fgetss () Return a line from the open file and filter out the asp and PHP tags.
file () Read the file into an array.
file_exists () Check if the file or directory exists.
file_get_contents () Read the file into a string.
file_put_contents () Write a string to a file.
fileatime () Returns the last access time of the file.
filectime () Returns the last modification time of the file.
filegroup () Returns the group ID of the file.
fileinode () Returns the inode number of the file.
filemtime () Returns the last modification time of the file contents.
fileowner () Returns the user ID (owner) of the file.
fileperms () Returns the permissions of the file.
filesize () Returns the file size.
filetype () Returns the file type.
flock () Lock or release files.
fnmatch() Matches a file name or string based on a specified pattern.
fopen () Open a file or URL.
fpassthru () Read data from the open file to the end of the file (EOF) and write the result to the output buffer
fputcsv () Format the lines as CSV and write to an open file.
fputs () An alias for fwrite ().
fread () Read the open file.
fscanf () Parse the input according to the specified format.
fseek () Navigate in the open file.
fstat () Returns information about an open file.
ftell () Returns the current position in the open file.
ftruncate () Truncate the open file to the specified length.
fwrite() Write to open file.
glob () Returns an array containing filenames /directories that match the specified pattern.
is_dir () Determines whether the file is a directory.
is_executable () Check if the file is executable.
is_file () Check if the file is a regular file.
is_link () Check if the file is a link.
is_readable () Check if the file is readable.
is_uploaded_file () Determine if the file was uploaded via HTTP POST.
is_writable () Determines if the file is writable.
is_writeable () An alias for is_writable ().
lchgrp () Change group ownership of symbolic links.
lchown () Change user ownership of symbolic links.
link() Create a hard link.
linkinfo () Returns information about a hard link.
lstat () Returns information about a file or symbolic link.
mkdir () Create a directory.
move_uploaded_file () Move the uploaded file to a new location.
parse_ini_file () Parse a configuration file.
parse_ini_string () Parse a configuration string.
pathinfo () Returns information about the file path.
pclose () Close the process opened by popen ().
popen () Open a process.
readfile () Read a file and write to the output buffer.
readlink () Returns the target of a symbolic link.
realpath () Returns the absolute path name.
realpath_cache_get() Returns the cache entry.
realpath_cache_size () Returns the cache size.
rename () Rename the file or directory.
rewind () Rewind the position of the file pointer.
rmdir () Delete the empty directory.
set_file_buffer () Set the buffer size of the opened file.
stat () Returns information about the file.
symlink () Create a symbolic link.
tempnam () Create a unique temporary file.
tmpfile () Create a unique temporary file.
touch () Set the access and modification time of the file.
umask () Change the file permissions of the file.
unlink () Delete the file.