PHP allows you to read, write, and manipulate files on your server. Here are a few important functions:
To open a file, use the fopen()
function:
<?php
$file = fopen("example.txt", "r");
if ($file) {
echo "File opened successfully.";
} else {
echo "Unable to open file.";
}
?>
To read the contents of a file, use fread()
:
<?php
$file = fopen("example.txt", "r");
$content = fread($file, filesize("example.txt"));
echo $content;
fclose($file);
?>
To write to a file, use fwrite()
:
<?php
$file = fopen("example.txt", "w");
fwrite($file, "Hello, this is a test file.");
fclose($file);
?>
Create a script that opens a file, reads its contents, and writes new data to the file.
Answers: Use fopen()
to open a file. Use fwrite()
to write to a file.