File Usage in PHP

Below is a quick guide on reading and writing files in PHP, including examples of both reading/writing a whole file using file_get_contents() and reading/writing one line at a time using fgets() and fwrite().

Reading and Writing Files in PHP

Files are essential for data storage and processing in web development. PHP provides versatile functions for reading and writing files, whether you need to manipulate text or binary data. In this tutorial, we'll explore the basics of working with files in PHP, including reading and writing files, handling binary and text files, serializing data, and reading/writing one line at a time.

Reading Files in PHP

Reading Text Files

To read a text file line by line, you can use the fopen(), fgets(), and fclose() functions:

$filename = "sample.txt";
$file = fopen($filename, "r");

if ($file) {
    while (($line = fgets($file)) !== false) {
        echo $line; // Process each line
    }
    fclose($file);
} else {
    echo "Unable to open file.";
}

This code opens the file, reads it line by line, and echoes each line.

Reading Binary Files

To read binary files (e.g., images), use file_get_contents():

$filename = "image.jpg";
$data = file_get_contents($filename);

if ($data !== false) {
    // Process binary data
} else {
    echo "Unable to read file.";
}

file_get_contents() reads the entire binary file into a variable, allowing you to process it as needed.

Reading Files Line by Line

Reading files line by line is useful for large files. You can use a loop with fgets():

$filename = "sample.txt";
$file = fopen($filename, "r");

if ($file) {
    while (($line = fgets($file)) !== false) {
        echo $line; // Process each line
    }
    fclose($file);
} else {
    echo "Unable to open file.";
}

Writing Files in PHP

Writing Text Files

To write to a text file, you can use the fopen(), fwrite(), and fclose() functions:

$filename = "output.txt";
$file = fopen($filename, "w");

if ($file) {
    $text = "Hello, World!";
    fwrite($file, $text);
    fclose($file);
} else {
    echo "Unable to open file for writing.";
}

This code opens a file, writes text to it, and closes it.

Writing Binary Files

To write binary data to a file, use file_put_contents():

$filename = "image.jpg";
$data = ...; // Binary data to write

if (file_put_contents($filename, $data) !== false) {
    // File successfully written
} else {
    echo "Unable to write file.";
}

Text vs. Binary Files

Serializing Data

PHP provides serialization functions for storing complex data structures in files:

Serialization (Storing Data)

$data = ["name" => "John", "age" => 30];
$serializedData = serialize($data);

file_put_contents("data.txt", $serializedData);

Unserialization (Retrieving Data)

$serializedData = file_get_contents("data.txt");
$data = unserialize($serializedData);

echo $data["name"]; // Outputs: John

Serialization is useful for storing and retrieving complex data structures, like arrays or objects.

Conclusion

Understanding how to work with files in PHP is fundamental for web development. Whether you need to read or write text or binary data, serialize data, or process files line by line, PHP provides versatile tools to help you achieve your goals. These skills are essential for building applications that involve file handling and data storage.