Contents
PHP Filesystem Functions Complete Reference
PHP Filesystem Functions Complete Reference
basename()
Description: Return the filename from a given path.
Example:
echo basename("/path/to/file.txt");
// Output: file.txt
chgrp()
Description: Change the user group of a specified file.
Example:
chgrp("example.txt", "users");
// Output: (changes the group to 'users')
chmod()
Description: Modify file permissions to a specified mode.
Example:
chmod("example.txt", 0755);
// Output: (sets permissions to 755)
chown()
Description: Assign a new owner to a specified file.
Example:
chown("example.txt", "newowner");
// Output: (changes the owner to 'newowner')
copy()
Description: Create a copy of a specified file.
Example:
copy("source.txt", "copy.txt");
// Output: (creates a duplicate of 'source.txt' as 'copy.txt')
dirname()
Description: Get the directory path from a file path.
Example:
echo dirname("/path/to/file.txt");
// Output: /path/to
disk_free_space()
Description: Determine the free space in a specified directory.Example:
echo disk_free_space("/");
// Output: (returns free space in bytes)
disk_total_space()
Description: Retrieve the total storage capacity of a specified directory.Example:
echo disk_total_space("/");
// Output: (returns total space in bytes)
fclose()
Description: Close a file pointer that is currently open.
Example:
$file = fopen("example.txt", "r");
fclose($file);
// Output: (closes 'example.txt')
feof()
Description: Check if the end-of-file has been reached on a file pointer.
Example:
$file = fopen("example.txt", "r");
while (!feof($file)) {
echo fgets($file);
}
fclose($file);
// Output: (reads until end of file)
fflush()
Description: Flush the output buffer to an open file.
Example:
$file = fopen("example.txt", "a");
fwrite($file, "New content");
fflush($file);
fclose($file);
// Output: (writes 'New content' and flushes buffer)
fgetc()
Description: Retrieve a single character from an open file.Example:
$file = fopen("example.csv", "w");
fputcsv($file, ["Name", "Age", "City"]); // Writes CSV line
fclose($file);
fgets()
Description: Read a line from a file pointer.Example:
$file = fopen("example.txt", "r");
echo fgets($file);
fclose($file);
// Output: (reads and outputs the first line)
fgetss()
Description: Read a line from a file while stripping out HTML and PHP tags.
Example:
$file = fopen("example.txt", "r");
echo fgetss($file);
fclose($file);
// Output: (outputs the line without HTML tags)
file_exists()
Description: Check if a file or directory exists.
Example:
echo file_exists("example.txt") ? "Exists" : "Does not exist";
// Output: Exists or Does not exist
file_get_contents()
Description: Read the entire file content into a string.
Example:
echo file_get_contents("example.txt");
// Output: (displays file contents as a string)
file_put_contents()
Description: Write a string to a file.
Example:
file_put_contents("example.txt", "Some content");
// Output: (writes 'Some content' to 'example.txt')
fileatime()
Description: Retrieve the last access time of a specified file.
Example:
echo date("F d Y H:i:s", fileatime("example.txt"));
// Output: (displays last access date and time)
filesize()
Description: Get the size of a specified file in bytes.
Example:
echo filesize("example.txt");
// Output: (file size in bytes)
filectime()
Description: Get the last change time of a specified file.
Example:
echo date("F d Y H:i:s", filectime("example.txt"));
// Output: (displays last change date and time)
filemtime()
Description:Return the last modified time of a specified file.
Example:
echo date("F d Y H:i:s", filemtime("example.txt"));
// Output: (displays last modified date and time)
fileperms()
Description: Retrieve the permissions for a file or directory.
Example:
echo substr(sprintf('%o', fileperms("example.txt")), -4);
// Output: (displays permissions, e.g., 0755)
filesize()
Description: Get the size of a specified file in bytes.
Example:
echo filesize("example.txt");
// Output: (file size in bytes)
filetype()
Description: Determine the type of file.
Example:
echo filetype("example.txt");
// Output: file or dir
flock()
Description: Apply a portable advisory lock on the file.
Example:
$file = fopen("example.txt", "r+");
if (flock($file, LOCK_EX)) {
// Do something
flock($file, LOCK_UN);
}
fclose($file);
// Output: (locks and unlocks the file)
fopen()
Description: Open a file or URL.
Example:
$file = fopen("example.txt", "r");
// Output: (opens 'example.txt' for reading)
fpassthru()
Description: Output all remaining data from a file pointer until EOF.
Example:
The variable is an integer.
The variable is a string.
fputcsv()
Description: Write a line of CSV formatted data to a file.
Example:
$file = fopen("data.csv", "w");
fputcsv($file, ["Name", "Age"]);
fclose($file);
// Output: (writes "Name, Age" to 'data.csv')
fread()
Description: Read up to a specified length of bytes from a file.
Example:
$file = fopen("example.txt", "r");
echo fread($file, 100);
fclose($file);
// Output: (displays first 100 bytes)
fseek()
Description: Set the file pointer to a specified position.
Example:
$file = fopen("example.txt", "r");
fseek($file, 5);
echo fgets($file);
fclose($file);
// Output: (displays content starting from position 5)
fstat()
Description: Return information about an open file.
Example:
$file = fopen("example.txt", "r");
print_r(fstat($file));
fclose($file);
// Output: (displays file info array)
ftell()
Description: Get the current position of the file pointer.
Example:
$file = fopen("example.txt", "r");
echo ftell($file);
fclose($file);
// Output: (current file pointer position)
ftruncate()
Description: Truncate a file to a specified length.
Example:
$file = fopen("example.txt", "r+");
ftruncate($file, 50);
fclose($file);
// Output: (shortens file to 50 bytes)
fwrite()
Description: Write a string to a file.
Example:
$file = fopen("example.txt", "w");
fwrite($file, "PHP Filesystem Functions");
fclose($file);
// Output: (writes text to 'example.txt')
is_dir()
Description: Check if a path is a directory.
Example:
echo is_dir("/path/to/directory") ? "Directory" : "Not a directory";
// Output: Directory or Not a directory
is_executable()
Description: Determine if a file is executable.
Example:
echo is_executable("script.sh") ? "Executable" : "Not executable";
// Output: Executable or Not executable
is_file()
Description: Verify if a path is a regular file.
Example:
echo is_file("example.txt") ? "File" : "Not a file";
// Output: File or Not a file
is_link()
Description: Check if a specified file is a symbolic link.
Example:
echo is_link("example_link") ? "Symbolic link" : "Not a symbolic link";
// Output: Symbolic link or Not a symbolic link
is_readable()
Description: Determine if a file exists and is readable.
Example:
echo is_readable("example.txt") ? "Readable" : "Not readable";
// Output: Readable or Not readable
is_uploaded_file()
Description: Check if a file was uploaded via HTTP POST.
Example:
echo is_uploaded_file($_FILES['file']['tmp_name']) ? "Uploaded" : "Not uploaded";
// Output: Uploaded or Not uploaded
is_writable()
Description: Verify if a file is writable.
Example:
echo is_writable("example.txt") ? "Writable" : "Not writable";
// Output: Writable or Not writable
link()
Description: Create a hard link for a specified target.
Example:
link("target.txt", "hardlink.txt");
// Output: (creates a hard link 'hardlink.txt' pointing to 'target.txt')
lstat()
Description: Retrieve information about a file or symbolic link.
Example:
print_r(lstat("example.txt"));
// Output: (array of file or symbolic link information)
popen()
Description: Open a pipe to a specified command.
Example:
$pipe = popen("ls", "r");
// Output: (opens a pipe to list directory contents)
pclose()
Description: Close a pipe opened by popen()
.
Example:
$pipe = popen("ls", "r");
pclose($pipe);
// Output: (closes the pipe to the command 'ls')
pathinfo()
Description: Get information about a path as an associative array or string.
Example:
print_r(pathinfo("example.txt"));
// Output: (displays path info, e.g., directory, basename, extension)
readfile()
Description: Read a file and output it directly to the buffer.
Example:
readfile("example.txt");
// Output: (displays contents of 'example.txt')
rename()
Description: Rename a file or directory.
Example:
rename("oldname.txt", "newname.txt");
// Output: (changes 'oldname.txt' to 'newname.txt')
realpath()
Description: Return the canonical absolute pathname.
Example:
echo realpath("example.txt");
// Output: (full path to 'example.txt')
rewind()
Description: Set the file pointer to the beginning of a file.
Example:
$file = fopen("example.txt", "r");
rewind($file);
fclose($file);
// Output: (moves file pointer to start)
stat()
Description: Retrieve detailed information about a file.
Example:
print_r(stat("example.txt"));
// Output: (array of file statistics)
symlink()
Description: Create a symbolic link pointing to a specified target.
Example:
symlink("target.txt", "symbolic_link.txt");
// Output: (creates symbolic link 'symbolic_link.txt' to 'target.txt')
tmpfile()
Description: Create a temporary file in read-write mode.
Example:
$temp = tmpfile();
fwrite($temp, "Temporary data");
fclose($temp);
// Output: (creates and closes a temporary file)
touch()
Description: Set the access and modification times for a file.
Example:
touch("example.txt");
// Output: (updates 'example.txt' timestamp)
unlink()
Description: Delete a file.
Example:
unlink("example.txt");
// Output: (removes 'example.txt')