PHP : From Basic To Advance

Day 10-11: Working with Files in PHP

Handling files is a common task in PHP, allowing you to read from and write to files, which is essential for many web applications.

Reading from a File

You can read from a file using functions like fopen, fread, and fgets.

1. Reading an Entire File

Use the file_get_contents function to read the entire content of a file into a string.

[mycode]                   

<?php
// Read the entire file content into a string
$filename = ‘example.txt’;
$fileContent = file_get_contents($filename);

// Display the content
echo nl2br($fileContent); // nl2br() to preserve new lines
?>

[/mycode]

2. Reading a File Line by Line

Use the fgets function to read a file line by line.

[mycode]                   

<?php
$filename = ‘example.txt’;
$file = fopen($filename, ‘r’); // Open the file in read mode

if ($file) {
    while (($line = fgets($file)) !== false) {
        echo nl2br($line); // Display each line
    }
    fclose($file); // Close the file
} else {
    echo “Error opening the file.”;
}
?>

[/mycode]

3. Reading a File into an Array

Use the file function to read a file into an array, where each line is an element of the array.

[mycode]                   

<?php
$filename = ‘example.txt’;
$lines = file($filename); // Read file into an array

foreach ($lines as $line) {
    echo nl2br($line); // Display each line
}
?>

[/mycode]

Writing to a File

You can write to a file using functions like fopen, fwrite, and fputs.

1. Writing a String to a File

Use the file_put_contents function to write a string to a file.

[mycode]                   

<?php
$filename = ‘example.txt’;
$content = “This is a new content.\n”;

// Write the content to the file (overwrites existing content)
file_put_contents($filename, $content);

// Append the content to the file
file_put_contents($filename, $content, FILE_APPEND);
?>

[/mycode]

2. Writing Data Line by Line

Use the fwrite function to write data to a file.

[mycode]                   

<?php
$filename = ‘example.txt’;
$file = fopen($filename, ‘w’); // Open the file in write mode

if ($file) {
    $content = “First line.\n”;
    fwrite($file, $content); // Write content to the file

    $content = “Second line.\n”;
    fwrite($file, $content); // Write more content to the file

    fclose($file); // Close the file
} else {
    echo “Error opening the file.”;
}
?>

[/mycode]

Example: Combining Reading and Writing

Let’s create a script that reads a file, appends new content, and then reads the updated file.

1. Initial File Content

Create an initial file example.txt with some content.

[mycode]                   

Line 1: Original content.
Line 2: More original content.

[/mycode]

2. PHP Script to Append and Read File

Create a PHP script append_and_read.php:

[mycode]                   

<?php
$filename = ‘example.txt’;

// Read and display the current content
echo “<h3>Current File Content:</h3>”;
$currentContent = file_get_contents($filename);
echo nl2br($currentContent);

// Append new content to the file
$newContent = “Line 3: Appended content.\n”;
file_put_contents($filename, $newContent, FILE_APPEND);

// Read and display the updated content
echo “<h3>Updated File Content:</h3>”;
$updatedContent = file_get_contents($filename);
echo nl2br($updatedContent);
?>

[/mycode]


Leave a Reply

Scroll to Top