Welcome to File Handling in PHP!
PHP allows us to create, read, write, and modify files, making it useful for logging data, storing configurations, or managing content dynamically. In this challenge, we will explore how to list files in a directory, read from files, and write to files using PHP.
For this activity, you will be using PHPStorm and your local MAMP web server to test your code. Make sure MAMP is running and your project is configured correctly in PHPStorm.
Before working with files, let’s first display the files inside a directory. Create a file called list_files.php and add the following code:
<?php
$directory = "."; // Current directory
$files = scandir($directory);
echo "<h3>Files in the directory:</h3>";
foreach ($files as $file) {
if ($file !== "." && $file !== "..") {
echo $file . "<br>";
}
}
?>Run list_files.php in your browser to see a list of files in the project directory.
Modify the script to list files from a specific subdirectory (e.g., "documents" instead of ".").
Now, let’s create and write data to a file. Create a file called write_file.php and add this:
<?php
$filename = "sample.txt";
// Open the file for writing ('w' mode erases the content if it exists)
$file = fopen($filename, "w");
// Content to write
$text = "Hello, this is a test file!\nWelcome to PHP file handling.\n";
// Write to file
fwrite($file, $text);
// Close file
fclose($file);
echo "Data written to $filename successfully.";
?>Run write_file.php and check your project folder to confirm that sample.txt has been created.
Modify the script to append data instead of overwriting it ("a" mode instead of "w").
Now, let’s read the content of the file we just created. Create a file called read_file.php:
<?php
$filename = "sample.txt";
// Check if file exists before reading
if (file_exists($filename)) {
$file = fopen($filename, "r");
echo "<h3>Contents of $filename:</h3>";
while (!feof($file)) {
echo fgets($file) . "<br>";
}
fclose($file);
} else {
echo "Error: File not found.";
}
?>Run read_file.php in your browser and confirm it displays the content of sample.txt.
Modify the script to read the entire file into a single string using file_get_contents().
If we no longer need a file, we can delete it using unlink(). Create delete_file.php:
<?php
$filename = "sample.txt";
if (file_exists($filename)) {
unlink($filename);
echo "$filename has been deleted.";
} else {
echo "Error: File not found.";
}
?>Run delete_file.php, then check if sample.txt is still in the directory.
Modify the script to check for user confirmation before deleting the file.
Great job! You’ve just explored how to list, create, write, read, and delete files in PHP. Keep experimenting with different file operations to enhance your PHP skills!
You will complete the scripting in the supplied script template file according to the instructions in the video, so that the script will read from the text file and save to the text file any time the button is clicked or the page is reloaded.
Script Template
Starter Text File
Please submit the script file.