Chapter 8: Working with Files and Directories
Learn how to manage files and directories in PHP, including reading, writing, and handling file uploads.
In this chapter, we’ll explore how to work with files and directories in PHP. Managing files is essential for tasks such as storing data, generating reports, and handling user-uploaded files. We’ll cover reading, writing, and uploading files, as well as directory operations.
Reading Files
PHP provides several functions for reading files. The file_get_contents
function is a simple way to read the entire content of a file into a string:
<?php
$content = file_get_contents("example.txt");
echo $content;
?>
In this example, file_get_contents
reads the content of example.txt
and displays it on the page.
Writing to Files
To write data to a file, use file_put_contents
, which writes a string to a file and creates the file if it doesn’t exist:
<?php
$data = "This is some sample text.";
file_put_contents("output.txt", $data);
echo "Data written to file.";
?>
Here, file_put_contents
writes $data
to output.txt
, creating the file if it doesn’t already exist.
Appending Data to Files
To add data to an existing file without overwriting it, use the FILE_APPEND
flag with file_put_contents
:
<?php
$newData = "Additional content.\n";
file_put_contents("output.txt", $newData, FILE_APPEND);
echo "Data appended to file.";
?>
This example appends $newData
to the end of output.txt
.
Handling File Uploads
PHP allows users to upload files through HTML forms. To handle uploads, first create a form with enctype="multipart/form-data"
:
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Choose file:</label>
<input type="file" name="file" id="file">
<input type="submit" value="Upload">
</form>
This form sends the file data to upload.php
when submitted.
Processing File Uploads
In upload.php
, use the $_FILES
superglobal to access uploaded file data and move it to a permanent directory:
<?php
if (isset($_FILES['file'])) {
$targetDir = "uploads/";
$targetFile = $targetDir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $targetFile)) {
echo "File uploaded successfully.";
} else {
echo "File upload failed.";
}
}
?>
This code checks if a file has been uploaded, then moves it to the uploads/
directory.
Working with Directories
PHP includes functions to work with directories. Use scandir
to list files in a directory:
<?php
$files = scandir("uploads/");
foreach ($files as $file) {
echo $file . "<br>";
}
?>
This example lists all files in the uploads/
directory.
Deleting Files
To delete a file, use the unlink
function:
<?php
$file = "output.txt";
if (file_exists($file)) {
unlink($file);
echo "File deleted.";
} else {
echo "File does not exist.";
}
?>
This code deletes output.txt
if it exists.
Summary and Next Steps
In this chapter, we covered file and directory operations in PHP, including reading, writing, appending, handling uploads, and managing directories. In the next chapter, we’ll dive into working with arrays and objects to manage collections of data efficiently in PHP.