From the course: PHP Techniques: Working with Files and Directories
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Reading line by line - PHP Tutorial
From the course: PHP Techniques: Working with Files and Directories
Reading line by line
- [Instructor] We know how to read characters and how to read an entire file. Often it's useful to read a file one line at a time. The PHP function fgets returns the next full line from a file. That's one line, all of the texts up until the next line return. It's useful when we want to read a file line by line. Most often you would use it inside a loop. It keeps track of which line is the next line using something called a file pointer which we'll discuss a little later. A classic use would be to read a CSV file where each row contains comma separated values. And then the PHP code can work with each line one at a time. Let's try it. To begin let's switch to fileaccess.php and select all and copy that code again. And then let's open a new file which we will save as file_lines.php and then I'll paste that code in there. I'll also just move this after file read. Instead of working with assetsloremipsum.txt, let's change…
Contents
-
-
-
(Locked)
Filesystem basics6m 6s
-
Relative paths and magic constants4m 53s
-
(Locked)
Accessing files5m 4s
-
(Locked)
Reading from files4m 52s
-
(Locked)
Reading line by line5m 9s
-
(Locked)
File permissions5m 34s
-
(Locked)
Writing to files5m 5s
-
(Locked)
Writing line returns2m 28s
-
(Locked)
Deleting files2m 38s
-
(Locked)
Moving the file pointer7m 36s
-
(Locked)
Examining file details6m 52s
-
(Locked)
Challenge: Create a log file2m 2s
-
(Locked)
Solution: Create a log file3m 17s
-
(Locked)
-
-
-