PHP's file system functions provide a powerful set of tools for working with files and directories on the web server. Whether it's creating, accessing, or manipulating files, PHP enables server-side operations that are essential for various web applications.
1. File Handles (fopen, fclose):
fopen() // Opens a file or a URL and returns a file pointer resource.
fclose() // Close a file or a URL
fopen
fopen(filename, mode)
'r'
: Read only'w'
: Write only, truncates or creates a file'a'
: Write only, appends or creates a file'r+'
: Read and write'w+'
: Read and write, truncates or creates a file'a+'
: Read and write, appends or creates a filefclose
fclose(file_handle)
<?php
$fileHandle = fopen("example.txt", "w"); // Open file for writing
if ($fileHandle) {
fwrite($fileHandle, "Hello, Codersmile!"); // Write to the file
fclose($fileHandle); // Close the file
echo "File written and closed successfully.";
} else {
echo "Failed to open the file.";
}
?>
fread() // Reads a specified number of bytes from a file.
Fgets() // Reads a line from a file.
File_get_contents() // Reads the entire content of a file into a string.
fwrite() //Writes the contents of a string to a file.
File_puRt_contents() // Writes data to a file. It appends content to the end
file_exits() //Checks whether a file or directory exists.
filesize() // Returns the size of the file.
filetype() // Returns the file type.
filectime() // Returns the creation time of the file.
filemtime() // Returns the last modification time of the file.
unlink() // Deltes a file.