PHP Filesystem Functions (2024)

❮ PreviousNext ❯

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.

Unix / Windows Compatibility

When specifying a path on Unix platforms, a forward slash (/) is used as directory separator.

On Windows platforms, both forward slash (/) and backslash (\) can be used.

Runtime Configuration

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

Name Default Description Changeable
allow_url_fopen "1" Allows fopen()-type functions to work with URLs PHP_INI_SYSTEM
allow_url_include "0" (available since PHP 5.2) 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 email address to be used on unauthenticated FTP connections and in the From header for HTTP connections when using ftp and http wrappers 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
sys_temp_dir "" (available since PHP 5.5) PHP_INI_SYSTEM

PHP Filesystem Functions

Function Description
basename() Returns the filename component of a path
chgrp() Changes the file group
chmod() Changes the file mode
chown() Changes the file owner
clearstatcache() Clears the file status cache
copy() Copies a file
delete() See unlink()
dirname() Returns the directory name component of a path
disk_free_space() Returns the free space of a filesystem or disk
disk_total_space() Returns the total size of a filesystem or disk
diskfreespace() Alias of disk_free_space()
fclose() Closes an open file
feof() Checks if the "end-of-file" (EOF) has been reached for an open file
fflush() Flushes buffered output to an open file
fgetc() Returns a single character from an open file
fgetcsv() Returns a line from an open CSV file
fgets() Returns a line from an open file
fgetss() Deprecated from PHP 7.3. Returns a line from an open file - stripped from HTML and PHP tags
file() Reads a file into an array
file_exists() Checks whether or not a file or directory exists
file_get_contents() Reads a file into a string
file_put_contents() Writes data to a file
fileatime() Returns the last access time of a file
filectime() Returns the last change time of a file
filegroup() Returns the group ID of a file
fileinode() Returns the inode number of a file
filemtime() Returns the last modification time of a file
fileowner() Returns the user ID (owner) of a file
fileperms() Returns the file's permissions
filesize() Returns the file size
filetype() Returns the file type
flock() Locks or releases a file
fnmatch() Matches a filename or string against a specified pattern
fopen() Opens a file or URL
fpassthru() Reads from the current position in a file - until EOF, and writes the result to the output buffer
fputcsv() Formats a line as CSV and writes it to an open file
fputs() Alias of fwrite()
fread() Reads from an open file (binary-safe)
fscanf() Parses input from an open file according to a specified format
fseek() Seeks in an open file
fstat() Returns information about an open file
ftell() Returns the current position in an open file
ftruncate() Truncates an open file to a specified length
fwrite() Writes to an open file (binary-safe)
glob() Returns an array of filenames / directories matching a specified pattern
is_dir() Checks whether a file is a directory
is_executable() Checks whether a file is executable
is_file() Checks whether a file is a regular file
is_link() Checks whether a file is a link
is_readable() Checks whether a file is readable
is_uploaded_file() Checks whether a file was uploaded via HTTP POST
is_writable() Checks whether a file is writable
is_writeable() Alias of is_writable()
lchgrp() Changes the group ownership of a symbolic link
lchown() Changes the user ownership of a symbolic link
link() Creates a hard link
linkinfo() Returns information about a hard link
lstat() Returns information about a file or symbolic link
mkdir() Creates a directory
move_uploaded_file() Moves an uploaded file to a new location
parse_ini_file() Parses a configuration file
parse_ini_string() Parses a configuration string
pathinfo() Returns information about a file path
pclose() Closes a pipe opened by popen()
popen() Opens a pipe
readfile() Reads a file and writes it to the output buffer
readlink() Returns the target of a symbolic link
realpath() Returns the absolute pathname
realpath_cache_get() Returns realpath cache entries
realpath_cache_size() Returns realpath cache size
rename() Renames a file or directory
rewind() Rewinds a file pointer
rmdir() Removes an empty directory
set_file_buffer() Alias of stream_set_write_buffer(). Sets the buffer size for write operations on the given file
stat() Returns information about a file
symlink() Creates a symbolic link
tempnam() Creates a unique temporary file
tmpfile() Creates a unique temporary file
touch() Sets access and modification time of a file
umask() Changes file permissions for files
unlink() Deletes a file

❮ PreviousNext ❯

★ +1

W3schools Pathfinder

Track your progress - it's free!

Log in Sign Up

PHP Filesystem Functions (2024)

FAQs

What are the six functions of file handling in PHP? ›

PHP File System allows us to create file, read file line by line, read file character by character, write file, append file, delete file and close file.

What is PHP file reading functions? ›

Definition and Usage

The readfile() function reads a file and writes it to the output buffer. Tip: You can use a URL as a filename with this function if the fopen wrappers have been enabled in the php.

What choices do you have to make when opening a file in PHP? ›

The `fopen()` function is a built-in PHP function used for opening files or URLs. It takes two parameters - the filename and the mode in which the file should be opened. Some common file modes include 'r' for read-only mode, 'w' for write-only mode, and 'a' for write-only mode at the end of the file.

Which PHP function is used to find files? ›

PHP Filesystem Functions
FunctionDescription
glob()Returns an array of filenames / directories matching a specified pattern
is_dir()Checks whether a file is a directory
is_executable()Checks whether a file is executable
is_file()Checks whether a file is a regular file
77 more rows

What are the 4 main functions involved in file management? ›

The file manager functions can create, open and close files, write data to files, and read data from files. In addition, file manager routines can create directories, determine characteristics of files and directories, and copy files.

How many file handling modes are there in PHP? ›

File Opening Modes
ModesDescription
rRead only. Starts at the beginning of the file
r+Read/Write. Starts at the beginning of the file
wWrite only. Opens and clears the contents of file; or creates a new file if it doesn't exist
w+Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
4 more rows

What is the correct way to open a file in PHP? ›

PHP provides several functions to facilitate file operations. To open a file, the fopen() function is used, which takes two parameters: the filename (including the path) and the mode (such as "r" for reading or "w" for writing).

What do you need before running PHP files? ›

Before you run or start writing any program in PHP, you should start Apache and MYSQL. After starting both servers, you have to write a program in Notepad.

Which function is used to open a file in PHP? ›

The fopen() function opens a file or URL. Note: When writing to a text file, be sure to use the correct line-ending character! Unix systems use \n, Windows systems use \r\n, and Macintosh systems use \r as the line ending character.

Which function is used in PHP to delete a file? ›

The unlink() function deletes a file.

How to check if a file does not exist in PHP? ›

The file_exists() function in PHP is an inbuilt function which is used to check whether a file or directory exists or not. The path of the file or directory you want to check is passed as a parameter to the file_exists() function which returns True on success and False on failure.

How to check if a path exists in PHP? ›

The file_exists() function takes a file path as its parameter and returns a boolean value. It returns true if the file or directory exists, and false otherwise. It is a simple and effective way to check the existence of a file before proceeding with any operations on it.

What is the function of file handling? ›

Reusability: File handling allows us to preserve the information/data generated after we run the program. Saves Time: Some programs might require a large amount of input from their users. In such cases, file handling allows you to easily access a part of a code using individual commands.

What are the five functions of file management? ›

Functions of a file management system are as follows:
  • Store, arrange, or accessing files on a disk or other storage locations.
  • Creating new files.
  • Displaying the old files.
  • Adding and editing the data in files.
  • Moving files from one location to another.
  • Sorting files according to the given criteria.

What are the functions used in PHP in error handling? ›

PHP Error and Logging Functions
FunctionDescription
set_error_handler()Sets a user-defined error handler function
set_exception_handler()Sets a user-defined exception handler function
trigger_error()Creates a user-level error message
user_error()Alias of trigger_error()
8 more rows

What is the function PHP file? ›

Functions. php is a file that directly influences the functioning of your WordPress website. From it, you can create code patterns and add elements and functionality to the themes used in your pages. Thus, you are able to offer much more qualified experiences.

Top Articles
Latest Posts
Article information

Author: Kareem Mueller DO

Last Updated:

Views: 5957

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Kareem Mueller DO

Birthday: 1997-01-04

Address: Apt. 156 12935 Runolfsdottir Mission, Greenfort, MN 74384-6749

Phone: +16704982844747

Job: Corporate Administration Planner

Hobby: Mountain biking, Jewelry making, Stone skipping, Lacemaking, Knife making, Scrapbooking, Letterboxing

Introduction: My name is Kareem Mueller DO, I am a vivacious, super, thoughtful, excited, handsome, beautiful, combative person who loves writing and wants to share my knowledge and understanding with you.