File manipulation (Part-2)
In this PHP Tutorial you will learn the 2nd Part of File Manipulation Reading lines from a file, Reading arbitrary amounts of data from a file, Writing to a file, Creating directories, Removing a directory and Opening a directory for reading.
Reading lines from a file:
To read a line from an open file, you can use fgets().
fgets() requires the file resource returned from fopen() as an argument.
You may also pass fgets() an integer as a second argument.
The integer argument specifies the number of bytes that the function should read if it doesn’t first encounter a line end or the end of the file.
fgets() reads the file until it reaches a newline character ("\n"), the number of bytes specified in the length argument, or the end of the file.
$line = fgets($fp, 1024);
Although you can read lines with fgets(), you need some way to tell when you reach the end of the file.
The feof() function does this by returning true when the end of the file has been reached and false otherwise. feof() requires a file resource as its argument.
feof($fp);
Example:
<?php
$filename = “file.txt”;
fp = fopen($filename, ‘r’) or die(“Can’t open”);
while (!feof($fp)) {
$line = fgets($fp, 1024);
print “$line<br>”;
}
?>
Reading arbitrary amounts of data from a file:
Rather than reading text by the line, you can choose to read a file in arbitrarily defined chunks.
The fread() function accepts a file resource as an argument, as well as the number of bytes you want to read.
fread() returns the amount of data you requested, unless the end of the file is reached first.
Example:
<?php
$filename = “file.txt”;
$fp = fopen($filename, ‘r’) or die(“Can’t open”);
while (!feof($fp)) {
$chunk = fread($fp, 16);
print “$chunk<br>”;
}
?>
Writing to a file:
fwrite() accepts a file resource and a string, and then writes the string to the file. fputs() works in exactly the same way.
Example:
<?php
$filename = “file.txt”;
$fp = fopen($filename, ‘w’) or die(“Can’t open”);
fwrite($fp, “Written by:\n”);
fclose($fp);
$fp = fopen($filename, ‘a’) or die(“Can’t open”);
fputs($fp, “PHP\n”);
fclose($fp);
?>
Creating directories:
mkdir() enables you to create a directory.
mkdir() requires a string that represents the path to the directory you want to create, and an octal number integer that represents the mode you want to set for the directory.
You specify an octal (base 8) number with a leading 0. The mode argument has an effect only on Unix systems.
The mode should consist of three numbers between 0 and 7, representing permissions for the directory owner, group, and everyone, respectively.
mkdir() returns true if it successfully creates a directory, or false if it doesn’t. If mkdir() fails, it’s usually because the containing directory has permissions that preclude processes with the script’s user ID from writing.
mkdir(“dir”, 0777);
mkdir(“dir”, 0755);
Removing a directory:
rmdir() enables you to remove a directory from the file system if the directory is empty.
rmdir() requires only a string representing the path to the directory you want to create.
rmdir(“dir”);
Opening a directory for reading:
Before you can read the contents of a directory, you must first obtain a directory resource. You can do so with the opendir() function.
opendir() requires a string that represents the path to the directory you want to open. opendir() returns a directory handle unless the directory isn’t present or readable; in that case, it returns false.
$handle = opendir(“testdir”);