html","w", 1);
The final example opens a read-only stream to a remote index.html file:
$fh = fopen("http://www.example.com/", "r");
Of course, keep in mind fopen() only readies the resource for an impending operation.
Other than establishing the handle, it does nothing; you??™ll need to use other
functions to actually perform the read and write operations. These functions are
introduced in the sections that follow.
Closing a File
Good programming practice dictates that you should destroy pointers to any resources
once you??™re finished with them. The fclose() function handles this for you, closing
the previously opened file pointer specified by a file handle, returning TRUE on success
and FALSE otherwise. Its prototype follows:
boolean fclose(resource filehandle)
The filehandle must be an existing file pointer opened using fopen() or fsockopen().
Reading from a File
PHP offers numerous methods for reading data from a file, ranging from reading in just
one character at a time to reading in the entire file with a single operation. Many of
the most useful functions are introduced in this section.
290 CHAPTER 10 ?– WORKING WITH THE FI LE AND OPERATING SYSTEM
Reading a File into an Array
The file() function is capable of reading a file into an array, separating each element
by the newline character, with the newline still attached to the end of each element.
Its prototype follows:
array file(string filename [int use_include_path [, resource context]])
Although simplistic, the importance of this function can??™t be overstated, and therefore
it warrants a simple demonstration.
Pages:
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364