Module 12 - Working With Files

Topics in
this Module

Videos for Module 12 - Working With Files

There are no videos yet this term for this Module. Check back soon!

Activities for this Module

S12 - Playing With Files

Note: Sandbox assignments are designed to be formative activities that are somewhat open-ended. To get the most value, spend some time playing around as you code.

Sandbox Challenge: Playing with Files in PHP

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.

Part 1: Listing Files in a Directory

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 ".").

Part 2: Writing to a File

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").

Part 3: Reading from a File

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().

Part 4: Deleting a File

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! 

A12 - Text File Data Source

The Challenge

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.

Constraints:

  • You must use a self-submitting form.
  • The script must both read from and write to the text file.
  • User-friendly feedback should be displayed for both actions.

Resources

Script Template

Starter Text File

Submission Type

Please submit the script file.