PHP - Filesystem
The PHP has a number of methods and functions to access and manipulate the filesystem.
Installation
There is no installation needed to use these functions. These functions are part of the PHP core.
Runtime Configuration
The behavior of these functions is affected by settings in php.ini.
Filesystem and Streams Configuration Options
Name | Default | Description | Changeable |
---|---|---|---|
allow_url_fopen | "1" | This option enables the URL-aware fopen wrappers that enable accessing URL object like files. Default wrappers are provided for the access of remote files using the ftp or http protocol, some extensions like zlib may register additional wrappers. | PHP_INI_SYSTEM |
allow_url_include | "0" | This option allows the use of URL-aware fopen wrappers with the following functions: include, include_once, require, require_once. This setting requires allow_url_fopen to be on. | PHP_INI_SYSTEM Deprecated as of PHP 7.4.0. |
user_agent | NULL | Define the user agent for PHP to send. | PHP_INI_ALL |
default_socket_timeout | "60" | Default timeout (in seconds) for socket based streams. Specifying a negative value means an infinite timeout. | PHP_INI_ALL |
from | "" | The email address to be used on unauthenticated FTP connections and as the value of From header for HTTP connections, when using the ftp and http wrappers, respectively. | PHP_INI_ALL |
auto_detect_line_endings | "0" | When turned on, PHP will examine the data read by fgets() and file() to see if it is using Unix, MS-Dos or Macintosh line-ending conventions. | PHP_INI_ALL |
sys_temp_dir | "" | This enables PHP to interoperate with Macintosh systems, but defaults to Off, as there is a very small performance penalty when detecting the EOL conventions for the first line, and also because people using carriage-returns as item separators under Unix systems would experience non-backwards-compatible behavior. | PHP_INI_SYSTEM |
PHP Filesystem Functions
Functions | Description |
---|---|
basename() | Return filename from the specified path. |
chgrp() | Changes file group. |
chmod() | Changes file mode. |
clearstatcache() | Clears file status cache. |
chown() | Changes file owner. |
copy() | Copies file. |
delete() | See unlink() function or unset() function. |
dirname() | Returns a parent directory's path. |
disk_free_space() | Returns available space on filesystem or disk partition. |
disk_total_space() | Returns the total size of a filesystem or disk partition. |
diskfreespace() | Returns available space on filesystem or disk partition. Alias of disk_free_space() function. |
fclose() | Closes an open file pointer. |
feof() | Tests for end-of-file on a file pointer. |
fflush() | Flushes the output to a file. |
fgetc() | Gets character from file pointer. |
fgetcsv() | Gets line from file pointer and parse for CSV fields. |
fgets() | Gets line from file pointer. |
fgetss() | Gets line from file pointer and strip HTML tags. |
file() | Reads entire file into an array. |
file_exists() | Checks whether a file or directory exists. |
file_get_contents() | Reads entire file into a string. |
file_put_contents() | Write data to a file. |
fileatime() | Gets last access time of file. |
filectime() | Gets inode change time of file. |
filegroup() | Gets file group. |
fileinode() | Gets file inode. |
filemtime() | Gets file modification time. |
fileowner() | Gets file owner. |
fileperms() | Gets file permissions. |
filesize() | Gets file size. |
filetype() | Gets file type. |
flock() | Portable advisory file locking. |
fnmatch() | Match filename against a pattern. |
fopen() | Opens file or URL. |
fpassthru() | Output all remaining data on a file pointer. |
fputcsv() | Format line as CSV and write to file pointer. |
fputs() | Binary-safe file write. Alias of fwrite() function. |
fread() | Binary-safe file read. |
fscanf() | Parses input from a file according to a format. |
fseek() | Seeks on a file pointer. |
fstat() | Gets information about a file using an open file pointer. |
ftell() | Returns the current position of the file read/write pointer. |
ftruncate() | Truncates a file to a given length. |
fwrite() | Binary-safe file write. |
glob() | Find pathnames matching a pattern. |
is_dir() | Tells whether the filename is a directory. |
is_executable() | Tells whether the filename is executable. |
is_file() | Tells whether the filename is a regular file. |
is_link() | Checks whether the filename is a symbolic link. |
is_readable() | Tells whether a file exists and is readable. |
is_uploaded_file() | Checks whether the file was uploaded via HTTP POST. |
is_writable() | Tells whether the filename is writable. |
is_writeable() | Tells whether the filename is writable. Alias of is_writable() function. |
lchgrp() | Changes group ownership of symlink. |
lchown() | Changes user ownership of symlink. |
link() | Create a hard link |
linkinfo() | Gets information about a link. |
lstat() | Gives information about a file or symbolic link. |
mkdir() | Makes directory. |
move_uploaded_file() | Moves an uploaded file to a new location. |
parse_ini_file() | Parse a configuration file. |
parse_ini_string() | Parses a configuration string. |
pathinfo() | Returns information about a file path. |
pclose() | Closes process file pointer. |
popen() | Opens process file pointer. |
readlink() | Returns the target of a symbolic link. |
stat() | Gives information about a file. |
readfile() | Outputs a file. |
realpath() | Returns canonicalized absolute pathname. |
realpath_cache_get() | Get realpath cache entries. |
realpath_cache_size() | Get realpath cache size. |
rename() | Renames a file or directory. |
rewind() | Rewind the position of a file pointer. |
rmdir() | Removes directory |
set_file_buffer() | Sets write file buffering on the given stream. Alias of stream_set_write_buffer() function. |
symlink() | Creates a symbolic link. |
tempnam() | Create file with unique file name. |
tmpfile() | Creates a temporary file. |
touch() | Sets access and modification time of file. |
unlink() | Deletes a file. |
umask() | Changes the current umask. |
PHP Filesystem Predefined Constants
The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.
Constants | Type | Description |
---|---|---|
SEEK_SET | Integer | |
SEEK_CUR | Integer | |
SEEK_END | Integer | |
LOCK_SH | Integer | |
LOCK_EX | Integer | |
LOCK_UN | Integer | |
LOCK_NB | Integer | |
GLOB_BRACE | Integer | |
GLOB_ONLYDIR | Integer | |
GLOB_MARK | Integer | |
GLOB_NOSORT | Integer | |
GLOB_NOCHECK | Integer | |
GLOB_NOESCAPE | Integer | |
GLOB_AVAILABLE_FLAGS | Integer | |
PATHINFO_DIRNAME | Integer | |
PATHINFO_BASENAME | Integer | |
PATHINFO_EXTENSION | Integer | |
PATHINFO_FILENAME | Integer | |
FILE_USE_INCLUDE_PATH | Integer | Search for filename in include_path. |
FILE_NO_DEFAULT_CONTEXT | Integer | |
FILE_APPEND | Integer | Append content to existing file. |
FILE_IGNORE_NEW_LINES | Integer | Strip EOL characters. |
FILE_SKIP_EMPTY_LINES | Integer | Skip empty lines. |
FILE_BINARY | Integer | Binary mode. This constant has no effect, and is only available for forward compatibility. |
FILE_TEXT | Integer | Text mode. This constant has no effect, and is only available for forward compatibility. |
INI_SCANNER_NORMAL | Integer | Normal INI scanner mode. |
INI_SCANNER_RAW | Integer | Raw INI scanner mode. |
INI_SCANNER_TYPED | Integer | Typed INI scanner mode. |
FNM_NOESCAPE | Integer | Disable backslash escaping. |
FNM_PATHNAME | Integer | Slash in string only matches slash in the given pattern. |
FNM_PERIOD | Integer | Leading period in string must be exactly matched by period in the given pattern. |
FNM_CASEFOLD | Integer | Caseless match. Part of the GNU extension. |