Skip to content

Understanding file & directory

File handling in PHP is a common task that allows you to create, read, write, and delete files. PHP provides a variety of functions for file handling, making it easy to manage files on the server. Here’s an overview of the key functions and how to use them:

1. Opening a File

To open a file, use the fopen() function. This function takes two parameters: the filename and the mode in which to open the file.

Modes:

  • ‘r’ : Read only. Starts at the beginning of the file.
  • ‘r+’ : Read/Write. Starts at the beginning of the file.
  • ‘w’ : Write only. Opens and clears the contents of the file; or creates a new file if it doesn’t exist.
  • ‘w+’ : Read/Write. Opens and clears the contents of the file; or creates a new file if it doesn’t exist.
  • ‘a’ : Write only. Opens and writes to the end of the file or creates a new file if it doesn’t exist.
  • ‘a+’ : Read/Write. Preserves file content by writing to the end of the file.
  • ‘x’ : Write only. Creates a new file. Returns FALSE if file already exists.
  • ‘x+’ : Read/Write. Creates a new file. Returns FALSE if file already exists.

Example:

<?php

$filename = “example.txt”;

$file = fopen($filename, “r”) or die(“Unable to open file!”);

// Perform file operations

fclose($file);

?>

2. Reading a File

There are several functions to read the contents of a file:

  • fread(): Reads the file’s contents.
  • fgets(): Reads a single line from the file.
  • fgetcsv(): Reads a line from a CSV file and parses it into an array.
  • file_get_contents(): Reads the entire file into a string.

Example:

<?php

$filename = “example.txt”;

$file = fopen($filename, “r”) or die(“Unable to open file!”);

// Read file line by line

while(!feof($file)) {

    echo fgets($file) . “<br>”;

}

fclose($file);

// Read entire file into a string

$content = file_get_contents($filename);

echo $content;

?>

3. Writing to a File

To write to a file, use the fwrite() function. It takes the file handle and the string to write as parameters.

Example:

<?php

$filename = “example.txt”;

$file = fopen($filename, “w”) or die(“Unable to open file!”);

$text = “Hello, world!\n”;

fwrite($file, $text);

fclose($file);

?>

4. Appending to a File

To append data to a file, open the file in append mode (a or a+) and use fwrite().

Example:

<?php

$filename = “example.txt”;

$file = fopen($filename, “a”) or die(“Unable to open file!”);

$text = “Appending some text.\n”;

fwrite($file, $text);

fclose($file);

?>

5. Deleting a File

To delete a file, use the unlink() function.

Example:

<?php

$filename = “example.txt”;

if (file_exists($filename)) {

    unlink($filename) or die(“Unable to delete file!”);

    echo “File deleted successfully.”;

} else {

    echo “File does not exist.”;

}

?>

6. Checking if a File Exists

To check if a file exists, use the file_exists() function.

Example:

<?php

$filename = “example.txt”;

if (file_exists($filename)) {

    echo “The file $filename exists.”;

} else {

    echo “The file $filename does not exist.”;

}

?>

7. Getting File Information

To get information about a file, use functions like filesize(), filemtime(), filetype(), etc.

Example:

<?php

$filename = “example.txt”;

if (file_exists($filename)) {

    echo “File size: ” . filesize($filename) . ” bytes<br>”;

    echo “Last modified: ” . date(“F d Y H:i:s.”, filemtime($filename)) . “<br>”;

    echo “File type: ” . filetype($filename) . “<br>”;

}

?>

8. Closing a File

Always close the file after finishing the operations using fclose().

Example:

<?php

$filename = “example.txt”;

$file = fopen($filename, “r”) or die(“Unable to open file!”);

// Perform file operations

fclose($file);

?>

Putting It All Together

Here’s a complete example demonstrating various file handling operations:

<?php

$filename = “example.txt”;

// Create and write to the file

$file = fopen($filename, “w”) or die(“Unable to open file!”);

$text = “Hello, world!\n”;

fwrite($file, $text);

fclose($file);

// Append to the file

$file = fopen($filename, “a”) or die(“Unable to open file!”);

$append_text = “Appending some text.\n”;

fwrite($file, $append_text);

fclose($file);

// Read and output the file contents

$file = fopen($filename, “r”) or die(“Unable to open file!”);

while (!feof($file)) {

    echo fgets($file) . “<br>”;

}

fclose($file);

// Get file information

if (file_exists($filename)) {

    echo “File size: ” . filesize($filename) . ” bytes<br>”;

    echo “Last modified: ” . date(“F d Y H:i:s.”, filemtime($filename)) . “<br>”;

    echo “File type: ” . filetype($filename) . “<br>”;

}

// Delete the file

if (file_exists($filename)) {

    unlink($filename) or die(“Unable to delete file!”);

    echo “File deleted successfully.”;

} else {

    echo “File does not exist.”;

}

?>

This example covers the basic file handling operations in PHP, including creating, writing, reading, appending, checking file existence, getting file information, and deleting a file.