7.9. Reading and Writing to Files

Communication with files follows the pattern of opening a stream to a file, reading from or writing to it, and then closing the stream. When you open a stream, you get a resource that refers to the open stream. Each time you want to read from or write to the file, you use this stream identifier. Internally, PHP uses this integer to refer to all the necessary information for communicating with the file.

To open a file on the local file system, you use the fopen function. It takes a name of a file and a string that defines the mode of communication. This may be r for read-only or w for write-only, among other modes. It is also possible to specify an Internet address by starting the filename with http:// or ftp:// ...

Get Core PHP Programming, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.