Your path to becoming an Ethical Hacker! Hacking Academy Try It Now!

PHP Filesystem Functions

Discover a comprehensive list of PHP filesystem functions to read, write, and manage files and directories efficiently in your PHP projects.
The Filesystem function in PHP lets you work with files on your computer. These functions are built into PHP, so you don't need to install anything extra. When you want to access files, you use the file path. On Unix systems, you use a forward slash (/) to separate directories. On Windows, you can use either a forward slash (/) or a backward slash (\).

PHP Filesystem Functions


Syntax:


file_type function()

Example:

In this example, a file named `singleline.txt` contains one line of text: "This file consists of a single line."

PHP Code:


<?php
// Open the file using fopen() function
$check = fopen("singleline.txt", "r");
$seq = fgets($check);

// Write buffered output to a file until the end-of-file is reached
while (!feof($check))
    fflush($check);

// Close the file using fclose() function
fclose($check);
?>

Output:


This file consists of a single line.

By using these PHP filesystem functions, you can easily read, write, and manage files on your system.

Complete List of PHP Filesystem Functions:


PHP File System Functions Description
basename(path) Return the base name of the file. The base name is the filename without the path.
chgrp(filename, group) Change the group owner of a file to the specified group.
chmod(filename, mode) Change the mode (permissions) of a file to the specified mode.
chown(filename, owner) Change the owner of a file to the specified user.
copy(source_file, dest_file) Copy a file from one location to another.
dirname(path) Return the directory name of a given path.
disk_free_space(directory) Return the amount of free space available on the filesystem where the specified directory resides.
disk_total_space(directory) Return the total size of the filesystem where the specified directory resides.
fclose(file_handle) Close an open file pointer. It's important to close files after you're done with them to release resources.
feof(file_handle) Check if the end of a file has been reached for the file pointed to by the file handle.
fflush(file_handle) Flush the output to a file. This can be useful if you want to make sure data is written to the file immediately.
fgetc(file_handle) Read a single character from a file.
fgets(file_handle, length) Read a line from a file up to a specified length.
fgetss(file_handle, length) Read a line from a file and strip any HTML and PHP tags (similar to fgets).
file_exists(filename) Check whether a file or directory exists.
file_get_contents(filename) Read the entire contents of a file into a string.
file_put_contents(filename, data) Write a string to a file.
fileatime(filename) Return the last access time of a file.
filectime(filename) Return the last change time of a file.
filemtime(filename) Return the last modification time of a file.
fileperms(filename) Return the current permissions for a file or directory.
filesize(filename) Return the size of a file in bytes.
filetype(filename) Return the file type of a file or directory (e.g., "file", "dir").
SplFileObject::flock(mode) Apply portable advisory locks to a file using a SplFileObject object.
fnmatch(pattern, string) Perform a wildcard string match using a specified pattern.
fopen(filename, mode) Open a file (or URL) for reading, writing, or both.
fpassthru(file_handle) Output all remaining data from a file pointer.
fputcsv(file_handle, fields) Format a line as CSV (comma-separated values) and write it to an open file. The fields argument should be an array containing the values to be written.
fputs(file_handle, string) Write a string to an open file.
fread(file_handle, length) Read a specified length of bytes from a file.
fseek(file_handle, offset, whence) Set the file position indicator for a file. You can specify a relative or absolute offset, and from where the offset is measured.
fstat(file_handle) Get information about an open file using a file handle.
ftell(file_handle) Get the current position of the file pointer in an open file.
ftruncate(file_handle, size) Truncate a file to a certain size. This effectively removes data from the end of the file.
fwrite(file_handle, string) Write a string to an open file. Similar to fputs, but doesn't automatically add a newline character.
is_dir(filename) Check whether the specified path is a directory.
is_executable(filename) Check whether the specified file is executable.
is_file(filename) Check whether the specified path is a regular file.
is_link(filename) Check whether the specified path is a symbolic link.
is_readable(filename) Check whether the specified file exists and is readable.
is_uploaded_file(file) Check whether a file has been uploaded via an HTTP POST request.
is_writable(filename) Check whether the specified file is writable.
link(source, target) Create a hard link to a file.
lstat(filename) Get information about a file or symbolic link. Similar to stat, but follows symbolic links.
mkdir(pathname, mode) Create a new directory.
pathinfo(path, options) Return information about a path as an associative array or string.
pclose(handle) Close a pipe opened by popen.
popen(command, mode) Open a pipe to execute a command and return a file handle.
readfile(filename) Read a file and write it to the output buffer.
realpath(path) Return the canonicalized absolute pathname. This resolves any symbolic links and resolves the path to its final destination.
rename(oldname, newname) Rename a file or directory.
rewind(file_handle) Set the file position indicator for a file to the beginning of the file.
rmdir(dirname) Remove an empty directory.
stat(filename) Get information about a file.
symlink(target, link) Create a symbolic link to a target.
tmpfile() Create a temporary file with a unique name.
touch(filename, time) Set the access and modification time of a file.
unlink(filename) Delete a file.

Post a Comment

Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.