Книга: Fedora™ Unleashed, 2008 edition

Files

Files

As you will have learned from elsewhere in the book, the Unix philosophy is that every thing is a file. In PHP, this is also the case: A selection of basic file functions is suitable for opening and manipulating files, but those same functions can also be used for opening and manipulating network sockets. We cover both here.

Two basic read and write functions for files make performing these basic operations easy. They are file_get_contents(), which takes a filename as its only parameter and returns the file's contents as a string, and file_put_contents(), which takes a filename as its first parameter and the data to write as its second parameter.

Using these two, we can write a script that reads all the text from one file, filea.txt, and writes it to another, fileb.txt:

<?php
 $text = file_get_contents("filea.txt");
 file_put_contents("fileb.txt", $text);
?>

Because PHP enables us to treat network sockets like files, we can also use file_get_contents() to read text from a website, like this:

<?php
 $text = file_get_contents("http://www.slashdot.org");
 file_put_contents("fileb.txt", $text);
?>

The problem with using file_get_contents() is that it loads the whole file into memory at once; that's not practical if you have large files or even smaller files being accessed by many users. An alternative is to load the file piece by piece, which can be accomplished through the following five functions: fopen(), fclose(), fread(), fwrite(), and feof(). The f in those function names stands for file, so they open, close, read from, and write to files and sockets. The last function, feof(), returns true if the end of the file has been reached.

The fopen() function takes a bit of learning to use properly, but on the surface it looks straightforward. Its first parameter is the filename you want to open, which is easy enough. However, the second parameter is where you specify how you want to work with the file, and you should specify one of the following:

r — Read only; it overwrites the file

r+ — Reading and writing; it overwrites the file

w — Write only; it erases the existing contents and overwrites the file

w+ — Reading and writing; it erases the existing content and overwrites the file

a — Write only; it appends to the file

a+ — Reading and writing; it appends to the file

x — Write only, but only if the file does not exist

x+ — Reading and writing, but only if the file does not exist

Optionally, you can also add b (for example, a+b or rb) to switch to binary mode. This is recommended if you want your scripts and the files they write to work smoothly on other platforms.

When you call fopen(), you should store the return value. It is a resource known as a file handle, which the other file functions all need to do their jobs. The fread() function, for example, takes the file handle as its first parameter and the number of bytes to read as its second, returning the content in its return value. The fclose() function takes the file handle as its only parameter and frees up the file.

So, we can write a simple loop to open a file, read it piece by piece, print the pieces, and then close the handle:

<?php
 $file = fopen("filea.txt", "rb");
 while (!feof($file)) {
  $content = fread($file, 1024);
  echo $content;
 }
 fclose($file);
?>

That leaves only the fwrite() function, which takes the file handle as its first parameter and the string to write as its second. You can also provide an integer as the third parameter, specifying the number of bytes you want to write of the string, but if you exclude this, fwrite() writes the entire string.

If you recall, you can use a as the second parameter to fopen() to append data to a file. So we can combine that with fwrite() to have a script that adds a line of text to a file each time it is executed:

<?php
 $file = fopen("filea.txt", "ab");
 fwrite($file, "Testingn");
 fclose($file);
?>

To make that script a little more exciting, we can stir in a new function, filesize(), that takes a filename (not a file handle, but an actual filename string) as its only parameter and returns the file's size in bytes. Using that new function brings the script to this:

<?php
 $file = fopen("filea.txt", "ab");
 fwrite($file, "The filesize was" . filesize("filea.txt") . "n");
 fclose($file);
?>

Although PHP automatically cleans up file handles for you, it is still best to use fclose() yourself so that you are always in control.

Оглавление книги

Оглавление статьи/книги

Генерация: 0.065. Запросов К БД/Cache: 2 / 0
поделиться
Вверх Вниз