Chapter 10. Working with Files
The data storage destination of choice for a web application is a database. That doesn’t mean that you’re completely off the hook from dealing with regular old files, though. Plain text files are still a handy, universal way to exchange some kinds of information.
You can do some easy customization of your web site by storing HTML templates in text files. When it’s time to generate a specialized page, load the text file, substitute real data for the template elements, and print it. Example 10-1 shows you how to do this.
Files are also good for importing or exporting tabular data between your program and a spreadsheet. In your PHP programs, you can easily read and write the CSV (“comma-separated value”) files with which spreadsheet programs work.
Working with files in PHP also means working with remote web pages. A great thing about
file handling in PHP is you can open a remote file on another computer as easily as you can
open a file that sits on your web server. Most file-handling functions in PHP understand
URLs as well as local filenames. However, for this feature to work, the allow_url_fopen configuration directive must be enabled. It is
enabled by default, but if you’re having problems loading a remote file, check this
setting.
Understanding File Permissions
To read or write a file with any of the functions you’ll learn about in this chapter, the PHP interpreter must have permission from the operating system to do so. Every program that runs on a computer, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access