Workingwith files is easy in PHP. Files need not be located on the local machine to be accessible. It is also possible to work with remote files. In this section you learn to work with local and remote files efficiently. 3.6.1 Performing Basic Operations with Local FilesWorking with files is an important issue for every programming language. The same applies to PHP. Functions for accessing the filesystem allow the user to build highly sophisticated applications. With the help of files it is possible to store information about what's going on in your application, or you can access external data sources. Nowadays a lot of information is stored in databases, but files are still a fundamental component of every applications. This section guides you through PHP's functions related to files and filesystems. The first thing you have to know about files is how to open and close them: <?php $handle = @fopen("data.file", "r") or die ("cannot open file"); echo 'File has successfully been opened<br>'; fclose($handle); ?> In this example fopen and fclose are used. These functions are available in many programming languages and have been borrowed from C. In the script a file called data.file is opened for reading. This is done with the fopen function. The first parameter defines the name of the file that has to be opened. The second parameter tells PHP in which mode the file has to be opened. The following modes are supported by PHP:
Closing the file again is done by using fclose. Let's have a look at the content of the file the script has processed: Hans::Vienna::Database Developer Epi::Vienna::Consultant Kuli::Vienna::Sales Manager Sunny::Murau::Student The file consists of four lines. Let's see what you can find out about the file using a Unix command: [hs@athlon test]$ ls -l data.file -rw-r--r-- 1 hs cybertec 107 Oct 21 13:21 data.file The file is 107 bytes long and belongs to user hs in group cybertec. Sometimes it is necessary to find out even more about a file. Therefore PHP provides a command called stat: <?php $fileinfo = stat("data.file") or die ("cannot find information about file"); echo "device: $fileinfo[0]<br>\n"; echo "inode: $fileinfo[1]<br>\n"; echo "inode protection mode: $fileinfo[2]<br>\n"; echo "number of links: $fileinfo[3]<br>\n"; echo "user id of owner: $fileinfo[4]<br>\n"; echo "group id of owner: $fileinfo[5]<br>\n"; echo "device type if inode device: $fileinfo[6]<br>\n"; echo "size in bytes: $fileinfo[7]<br>\n"; echo "time of last access: $fileinfo[8]<br>\n"; echo "time of last modification: $fileinfo[9]<br>\n"; echo "time of last change: $fileinfo[10]<br>\n"; echo "blocksize for filesystem I/O: $fileinfo[11]<br>\n"; echo "number of block allocated: $fileinfo[12]<br>\n"; ?> If you execute the script, a lot of information will be displayed on the screen: device: 773 inode: 470215 inode protection mode: 33188 number of links: 1 user id of owner: 500 group id of owner: 500 device type if inode device: 2817 size in bytes: 107 time of last access: 1003663763 time of last modification: 1003663300 time of last change: 1003663300 blocksize for filesystem I/O: 4096 number of block allocated: 8 The output displayed by stat contains the same information that is also returned by the C function PHP's stat function is based on. To show you that PHP's functions for working with filesystems are based on C, we have included the structure returned by C's stat function: struct stat { dev_t st_dev; /* device */ ino_t st_ino; /* inode */ mode_t st_mode; /* protection */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device type (if inode device) */ off_t st_size; /* total size, in bytes */ unsigned long st_blksize; /* blocksize for filesystem I/O */ unsigned long st_blocks; /* number of blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last change */ }; As you can see, the content of the result generated by PHP is nearly equal to the result generated by C. This shows clearly that PHP and C are strongly related. Knowing this will make it easy for you to understand the behavior and the functions of the PHP interpreter. Up to now, you have learned to open and close files. You have also seen how to find out information about a file, but you haven't read and written data yet. Let's start with an example where you can see how data can be read from a file: <?php $file = "data.file"; $handle = fopen($file, "r") or die ("cannot open file"); $fileinfo = stat($file); $data = fgets($handle, $fileinfo[7]); echo "data using fgets: <br>$data<br><br>"; $data = fread($handle, $fileinfo[7]); echo "data using fread: <br>$data<br><br>"; rewind($handle); echo "Pointer is at position: ".ftell($handle)."<br>"; fseek($handle, 20); echo "Pointer is at position: ".ftell($handle); fclose($handle); ?> First, a file is opened and information about that file is retrieved by using stat. $fileinfo[7] contains the size of the file. Although you have passed the content of $fileinfo[7] to fgets, only the first line will be read because fgets interprets newline characters. After reading the first line, the pointer is set to the beginning of the second line. Then fread is used. This time the rest of the file will be read and displayed on the screen because fread does not take care of newline characters. After reading all data, the file pointer is at the end of the file. To perform further reads, it is necessary to set it to a different position. Therefore rewind is used to set the pointer to the first position in the file. After that fseek sets the pointer to the 20th position in the file. Fseek and rewind are two essential functions when working with files because you can use them to go to any position within the file you are working with. Let's have a look at the output of the script: data using fgets: Hans::Vienna::Database Developer data using fread: Epi::Vienna::Consultant Kuli::Vienna::Sales Manager Sunny::Murau::Student Pointer is at position: 0 Pointer is at position: 20 As you can see, the result is displayed on the screen correctly. The next example shows how data can be written to a file: <?php $file = "data.file2"; $handle = fopen($file, "w") or die ("cannot open file"); fputs($handle, "Hello World"); fclose($handle); ?> $handle contains the pointer of the new file created by fopen. Then fputs is calledand a string is written to the file before closing it. Up to now, you have only used rudimentary I/O functions. In some cases, it is more comfortable to read an entire file into an array: <?php $file = "data.file"; $data = file($file); echo '<table border="1"><tr>'; echo "<th>name</th><th>location</th><th>profession</th></tr>"; foreach ($data as $line) { $val = explode("::", $line); echo "<tr><td>$val[0]</td><td>$val[1]</td><td> $val[2]</td></tr>"; } echo "</table>"; ?> Reading a file into an array can be done with a function called file. To use file, it is not necessary to open the file first with fopen becausethis done implicitly by file. An array is returned. In the example the array is processed line by line and the string in the line is split after every ::. That way an array called $val is initialized every time the loop is processed and a table can be generated, which you can see in Figure 3.2. Figure 3.2. A simple table.If you want to display the content of the file on the screen without processing the data beforehand, you can use the readfile function instead. readfile(filename) opens a file and displays the content on the screen directly. This is very useful when the file already contains HTML code. 3.6.2 Performing Basic Operations with Remote FilesIn the previous section you have seen how local files can be treated with PHP. Remote files can be processed in a similar way. Here is the example you have just seen, but this time the data comes from a remote host: <?php $file = "http://212.186.25.254:/test/data.file"; $data = file($file); echo '<table border="1"><tr>'; echo "<th>name</th><th>location</th><th>profession</th></tr>"; foreach ($data as $line) { $val = explode("::", $line); echo "<tr><td>$val[0]</td><td>$val[1]</td><td> $val[2]</td></tr>"; } echo "</table>"; ?> As you can see, the only change that has to be made is that the position of the file is now defined by an URL. If you have the permissions to read that file, it can be processed like any other file. Every time the script is processed, however, the Web server will add one line to the logfile: 212.186.25.254 - - [21/Oct/2001:16:30:02 +0200] "GET /test/data.file HTTP/1.0" 200 107 "-" "PHP/4.0.4pl1" As we have already mentioned, working with remote files works just the same way as with local files. However, some important things have to be taken into consideration: <?php $file = "http://212.186.25.254:/test/data.file"; $handle = fopen($file, "a+") or die ("cannot open $file"); fputs($handle, "Pat::Indianapolis::Lawyer") or die ("cannot write to file"); fclose($handle) or die ("cannot close file"); echo "it works"; ?> Most people expect that the script will append data to the remote file, but this does not happen: [hs@athlon test]$ cat data.file Hans::Vienna::Database Developer Epi::Vienna::Consultant Kuli::Vienna::Sales Manager Sunny::Murau::Student Nothing has been appended to the remote file, although the output of the script is "it works" and no error has occurred while executing. This has to be taken into consideration when working with remote files; otherwise, you might wonder why your application returns no errors and produces no output. 3.6.3 Additional Filesystem FunctionsAs you have already seen, PHP offers functions for working with local and remote files. Up to now, you have seen how to perform all basic operations. In this section you will see that PHP provides a lot more functions than you have already seen:
As you can see, PHP provides an endless list of built-in functions. Most functions work with all common filesystems. Especially on Unix systems, PHP shows its tremendous power because some functions can only be used on Unix. |