PHP:
Writing a text file

How to:

PHP natively supports file writing through functions like file_put_contents, fopen together with fwrite, and fclose. Here is how to use them:

Simple Writing with file_put_contents:

This function simplifies the process of writing to a file by doing everything in one step.

$content = "Hello, world!";
file_put_contents("hello.txt", $content);
// Checks if the file is successfully written
if (file_exists("hello.txt")) {
    echo "File created successfully!";
} else {
    echo "Failed to create the file.";
}

Advanced Writing with fopen, fwrite, and fclose:

For more control over file writing, such as appending text or more error handling, use fopen with fwrite.

$file = fopen("hello.txt", "a"); // 'a' mode for append, 'w' for write
if ($file) {
    fwrite($file, "\nAdding more content.");
    fclose($file);
    echo "Content added successfully!";
} else {
    echo "Failed to open the file.";
}

Reading the File for Output:

To verify our content:

echo file_get_contents("hello.txt");

Sample Output:

Hello, world!
Adding more content.

Using Third-Party Libraries:

For more complex file operations, libraries such as League\Flysystem can be used for an abstraction layer over the file system, but PHP’s built-in functions are often sufficient for basic file writing tasks. Here’s a brief example if you choose to explore Flysystem:

require 'vendor/autoload.php';
use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;

$adapter = new LocalFilesystemAdapter(__DIR__);
$filesystem = new Filesystem($adapter);

$filesystem->write('hello.txt', "Using Flysystem to write this.");

This example assumes you’ve installed league/flysystem via Composer. Third-party libraries can greatly simplify more complex file handling, especially when working with different storage systems seamlessly.