Open In App

PHP Filesystem Functions Complete Reference

Improve
Improve
Like Article
Like
Save
Share
Report

The Filesystem function is used to access and manipulate filesystem. It is the part of PHP code so no need to install these functions. For accessing the files on the system, the file path will be used. On Unix system, forward slash (/) is used as a directory separator and on Windows platform, both forward slash (/) and backward slash (\) will be used.

Syntax:

file_type function()
 

Example: In the below program the file named singleline.txt contains a single line of information which is “This file consists of a single line.”.

PHP




<?php
 
// The file is opened using fopen() function
$check = fopen("singleline.txt", "r");
$seq = fgets($check);
 
// Writing buffered output to a file
// until the end-of-file is reached
while(! feof($check))
    fflush($check);
 
// The file is closed using fclose() function
fclose($check);
 
?>


Output:

This file consists of a single line.

Complete List of PHP Filesystem Functions:

Functions

Description

basename() Return the base name of a file if the path of the file is provided as a parameter to the basename() function.
chgrp() Change the user group of the specified file. 
chmod() Change the mode of a specified file to a specific mode given by the user.
chown() Change the owner of the specified file.
copy() It is used to make a copy of a specified file.
dirname() Return the directory name of a given path.
disk_free_space() Return the amount of free space in a specified directory.
disk_total_space() Return the total space of a specified directory.
fclose() Close a file pointed by an open file pointer.
feof()  Test for the end-of-file on a file pointer.
fflush() Write all the buffered output to an open file.
fgetc() Return a single character from an open file.
fgets() Return a line from a file pointer and it stops returning at a specified length.
fgetss() Return a line from an open file after removing HTML and PHP tags from the respective file.
file_exists() Check whether a file or directory exists or not.
file_get_contents() It is used to read a file into a string.
file_put_contents() It is used to write a string to a file.
fileatime() Return the last access time of a specified file.
filectime() Return the last time the specified file was changed.
filemtime() Return the last time of a specified file when its content was modified.
fileperms() Return the permissions given to a file or a directory.
filesize() Return the size of a specified file
filetype() Return the file type of a specified file or a directory.
SplFileObject flock() It is used to apply portable lock on the file.
fnmatch() Match a filename or string against a specified pattern.
fopen() (Function open file or URL) It is used to open a file or an URL.
fpassthru() Read data from a current position from a specified file until end of file .
fputcsv() Format a line as CSV(comma separated values) file and writes it to an open file
fputs() It is used to write to an open file.
fread() Reads up to length bytes from the file pointer referenced by file from an open file.
fseek() It is used to seek in an open file.
fstat() Return information about an open file.
ftell() Return the current position in an open file.
ftruncate() It is used to truncate(shorten) an open file to the specified length.
fwrite() It is used to write to an open file.
is_dir() Check whether the specified file is a directory or not.
is_executable() Check whether the specified file is an executable file or not.
is_file() Check whether the specified file is a regular file or not.
is_link() Check whether the specified file is a symbolic link or not.
is_readable() Check whether the specified file exists and is readable or not.
is_uploaded_file() Check whether the specified file uploaded via HTTP POST or not.
is_writable() Check whether the specified file is writable or not.
link() The link() creates a hard link for a specified target.
lstat() Return information about a file or a symbolic link.
mkdir() Creates a new directory with the specified pathname
pathinfo() Return information about a path using an associative array or a string.
pclose() The pclose() closes a pipe opened by the popen() function.
popen() Open a pipe to the program specified by the user using the command parameter.
readfile() Read a file and write it to the output buffer.
realpath() Return the canonicalized absolute pathname.
rename() It is used to rename a file or directory.
rewind() Set the position of the file pointer to the beginning of the file.
rmdir() It is used to remove an empty directory.
stat() It  is used to return information of a file
symlink() Create a symbolic link for a target that already exists.
tmpfile() Create a temporary file with a unique name in read-write (w+) mode.
touch() It is used setting the access and modification time of a specified file.
unlink() It is used to delete files. It is similar to UNIX unlink() function.


Last Updated : 25 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads