From the course: Learning PHP
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Reading files in PHP
- One of the ways to store and access data though it should absolutely be non-secure data, is by reading and writing to files. PHP allows you to read the contents of a file and if you have access to it, write to it as well. So first let's look at a couple of examples of reading files. On the screen here you see I have a file called treasure-island.txt, with a selection of lines from the Robert Louis Stevenson novel. There are two ways that we can read this file. The first is using a built-in function called readfile. So I can say readfile and then give it a file name, treasure-island.txt. This function simply grabs the contents of a file and puts it into the output buffer which is in most cases, immediately printed out onto the screen. Let's refresh our page here and see the results. You can see it's the entire file. And if we View Source, there is no formatting to it but it did break it up by lines. We just…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.