techmore.in

PHP - Files & I/O

Working with files in PHP involves various functions to read from, write to, and manipulate files on the filesystem. PHP provides a rich set of built-in functions for file handling and I/O operations. Here’s a comprehensive overview of how to work with files in PHP.

Opening and Closing Files

fopen()

Opens a file and returns a file handle (resource) which can be used for reading, writing, or both. The function requires the filename and the mode in which the file should be opened.

Modes:

  • r: Read only. Starts at the beginning of the file.
  • r+: Read and write. Starts at the beginning of the file.
  • w: Write only. Opens and truncates the file to zero length or creates a new file if it doesn't exist.
  • w+: Read and write. Opens and truncates the file to zero length 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 and write. Preserves file content by writing to the end of the file.
  • x: Write only. Creates a new file. Returns FALSE if the file already exists.
  • x+: Read and write. Creates a new file. Returns FALSE if the file already exists.

Example:

php
<?php $fileHandle = fopen("example.txt", "r"); if ($fileHandle) { // File opened successfully fclose($fileHandle); } else { // Error opening file } ?>

fclose()

Closes an open file handle.

Example:

php
<?php $fileHandle = fopen("example.txt", "r"); // Perform file operations fclose($fileHandle); ?>

Reading from Files

fread()

Reads a specified number of bytes from a file.

Example:

php
<?php $fileHandle = fopen("example.txt", "r"); if ($fileHandle) { $content = fread($fileHandle, filesize("example.txt")); fclose($fileHandle); echo $content; } ?>

fgets()

Reads a single line from a file.

Example:

php
<?php $fileHandle = fopen("example.txt", "r"); if ($fileHandle) { while (($line = fgets($fileHandle)) !== false) { echo $line; } fclose($fileHandle); } ?>

file_get_contents()

Reads the entire file into a string.

Example:

php
<?php $content = file_get_contents("example.txt"); echo $content; ?>

file()

Reads the entire file into an array, with each line as an array element.

Example:

php
<?php $lines = file("example.txt"); foreach ($lines as $line) { echo $line; } ?>

Writing to Files

fwrite()

Writes data to a file.

Example:

php
<?php $fileHandle = fopen("example.txt", "w"); if ($fileHandle) { fwrite($fileHandle, "Hello, World!"); fclose($fileHandle); } ?>

file_put_contents()

Writes data to a file. If the file does not exist, it will be created. If it exists, it will be truncated.

Example:

php
<?php file_put_contents("example.txt", "Hello, World!"); ?>

Other File Operations

fseek()

Sets the file pointer to a specified offset.

Example:

php
<?php $fileHandle = fopen("example.txt", "r"); if ($fileHandle) { fseek($fileHandle, 10); // Move file pointer to the 10th byte $content = fread($fileHandle, filesize("example.txt") - 10); fclose($fileHandle); echo $content; } ?>

feof()

Checks if the end of the file has been reached.

Example:

php
<?php $fileHandle = fopen("example.txt", "r"); if ($fileHandle) { while (!feof($fileHandle)) { $line = fgets($fileHandle); echo $line; } fclose($fileHandle); } ?>

File Information

filesize()

Returns the size of a file in bytes.

Example:

php
<?php $size = filesize("example.txt"); echo "File size: $size bytes"; ?>

file_exists()

Checks if a file or directory exists.

Example:

php
<?php if (file_exists("example.txt")) { echo "File exists."; } else { echo "File does not exist."; } ?>

File Permissions

chmod()

Changes the file mode (permissions).

Example:

php
<?php chmod("example.txt", 0644); // Read and write for owner, read-only for others ?>

unlink()

Deletes a file.

Example:

php
<?php if (file_exists("example.txt")) { unlink("example.txt"); echo "File deleted."; } else { echo "File does not exist."; } ?>

Directory Operations

opendir(), readdir(), closedir()

Open, read, and close directories.

Example:

php
<?php $dir = opendir("."); if ($dir) { while (($file = readdir($dir)) !== false) { echo "Filename: $file\n"; } closedir($dir); } ?>

Summary:

PHP provides comprehensive functions for file handling and I/O operations, allowing you to open, read, write, and manipulate files efficiently. Understanding these functions and how to use them is essential for effective file management in PHP applications.