Saturday, 25 June 2011

PHP FILES HANDLING - OPENING AND CLOSING FILES IN PHP





You can use the PHP function fopen in order to open files in PHP; practically, fopen() will allow you to associate a file with a file handle (which will then permit the manipulation of this file). The PHP syntax to follow is:


fopen(filename,mode),
  • where filename is the (string) path to the file you wish to open
  • where mode is one of the following opening modes:

  • 'r': this is the read-only mode; the file pointer is set at the beginning of the file.
  • 'r+': this is the reading/writing mode; the file pointer is set at the beginning of the file.
  • 'w': this denotes the write-only mode. The file is reduced to length 0 if it already exists; if the file doesn't already exist, it is created. The file pointer is set at the beginning of the file.
  • 'w+': this is the reading/writing mode. The file is reduced to length 0 if it already exists; if the file doesn't already exist, it is created. The file pointer is set at the beginning of the file.
  • 'a': same as the w mode, except that the file pointer is set at the end of the file.
  • 'a+': same as w+, except that the file pointer is set at the end of the file.
  • 'x': creates the file specified and opens it for writing only; the file pointer is set at the beginning of the file. If the file specified already exists, an error is returned.
  • 'x+': creates the file specified and opens it for reading and writing; the file pointer is set at the beginning of the file. If the file specified already exists, an error is returned.
Remark:

We advise you to append the letter b after the letter of the opening mode you chose as a parameter for fopen() (e.g. 'ab+' instead of'a+', 'rb' instead of 'r', etc ...). Indeed, this will giuarantee that the file is opened in binary mode and will prevent any compatibility problem.

PHP FILES HANDLING - PHP LOOPS ON FILE CONTENT WITH THE PHP FUNCTION FEOF


When you wish to run a PHP loop over the content of a file, you must ascertain that your PHP loop does terminate; in order to achieve this, either you know the exact length of your file (in bits or in characters), in which case you will be able to tell your PHP script to stop as soon as the file pointer goes past this length, or you don't know the file size, in which case you might be interested in a function which would allow you to test at each step whether the end of the file has been reached. Such a PHP function exists and is called feof(file end of file). It syntax is as follows:

feof(file_handle),

where file_handle is a PHP file handle as defined by fopen() in the previous paragraph.
This PHP function will return TRUE if the file pointer is pointing to the end of the file and FALSE otherwise

PHP FILES HANDLING - READING FILES IN PHP


There are different ways to read a file in PHP; one of them is to read it character by character, another way is to read it line by line.

Of course, both options are implicitly assuming that the file is a text file. If you plan to manipulate files which are not text files, then you shall not need to read them since the way they are encoded in memory is not easily readable by humans; however, some binary-safe functions exist that still allow you to output the value of one (resp. several) bytes as one (resp. several) characters (binary-safe means that there is a bijection, aka a one-to-one correspondence between the characters output and the binary value of those; therefore, you can manipulate/copy/modify this binary safe data without alienating the original binary data of the source). For instance, we have seen that fopen() can be made binary safe by adding a 'b' after the first letter of the opening mode.

Reading a file character by character in PHP:

The PHP function fgetc allows you to read a file character by character (the function is binary safe). The syntax to use is:
fgetc(file_handle)
  • where file_handle is the file handle defined by fopen.
Reading a file line by line in PHP:

The PHP function fgets allows you to read a file line by line (the function is binary safe). The syntax to use is:
fgets(file_handle, length)
  • where file_handle is the file handle as defined by fopen.
  • where length denotes the number of bytes to be read per line (if this parameter is not specified, the PHP script will run until the end of the line)
Reading a portion of a file in PHP:

The PHP function file_get_contents allows you to read a portion of a file in PHP (the function is binary safe), and returns it as a string. The syntax to use is:
file_get_contents(filename, TRUE, NULL, offset, maxlength)
  • where filename is the string filename of the file to be read
  • where offset denotes the location (in bytes from the beginning of the file) at which the script will start reading the file
  • where maxlength denotes the maximum number of bytes to be read (the script will keep reading the file until maxlength bytes have been read or until the end of file is reached, whichever comes first).

Remark:

If you are using PHP 6 or higher, you can keep the parameters TRUE and NULL as above; for simplicity reasons, we will not explain the various options offered for these parameters.




PHP FILES HANDLING - WRITING FILES IN PHP


Writing a portion of a file in PHP:


Using the fwrite PHP function, you can write the content of a string to a file (designated by a file handle as created with fopen). The fwrite function is binary-safe.
Combining this PHP function with the file_get_contents function (which returns a string), you can thus copy parts of one file into another (this works because both PHP functions are binary safe).
In the example below, we copy the content of the file initial.bmp (which must already exists) into a newly created file new.bmp:

<?php
$b = file_get_contents("initial.bmp",TRUE, NULL);
$a = fopen("new.bmp","wb+");
fwrite($a,$b);
?>

We have seen that PHP file handles allow you to open, read and write files. In order to preserve the binary integrity of files, one must use binary-safe functions. Currently, most PHP functions are binary-safe or can easily be made binary-safe.

No comments:

Post a Comment