PHP File Handling
PHP provides a rich set of functions for file handling. You can create, read, write, and manipulate files on the server using these functions.
Common File Handling Functions
fopen()
: Opens a file.fread()
: Reads from a file.fwrite()
: Writes to a file.fclose()
: Closes an open file.file_get_contents()
: Reads the entire file into a string.file_put_contents()
: Writes data to a file.
Example of File Handling
<?php
$file = 'example.txt';
// Create and write to a file
file_put_contents($file, 'Hello, World!');
// Read the file
$content = file_get_contents($file);
echo $content;
?>
Explanation: This example creates a file named example.txt
, writes "Hello, World!" to it, and then reads and displays the content of the file.