Chapter 10. File Handling
One of the most common design philosophies around Unix and Linux is that “everything is a file.” This means that, regardless of the resource with which you’re interacting, the operating system treats it as if it were a file locally on disk. This includes remote requests to other systems and handles on the output of processes running on the machine.
PHP treats requests, proceses, and resources similarly, but instead of considering everything to be a file, the language considers everything to be a stream resource. Chapter 11 covers streams at length, but the important point to know about streams for this chapter is the way PHP treats them in memory.
When accessing a file, PHP doesn’t necessarily read the file’s entire data into memory. Instead, it creates a resource
in memory that references the file’s location on disk and selectively buffers bytes from that file in memory. PHP then accesses or manipulates those buffered bytes directly as a stream. The fundamentals of streams, however, are not required knowledge for the recipes in this chapter.
PHP’s file methods—fopen()
, file_get_contents()
, and the like—all leverage the file://
stream wrapper under the hood. Remember, though, if everything in PHP is a stream, you can just as easily use other stream protocols as well, including php://
and http://
.
Windows Versus Unix
PHP is distributed for use on both Windows and Unix-style operating systems (including Linux and macOS). It’s important to understand that ...
Get PHP Cookbook 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.