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 from files - PHP Tutorial
From the course: PHP Techniques: Working with Files and Directories
Reading from files
- [Instructor] Now that we know how to open files, let's learn how to read data from them. There are three techniques for reading files that I want to show you. This movie is going to cover the first two. The first we'll look at is PHP's fread function. Fread takes two arguments, the handle of an open file and the number of bytes we want to read. In most character sets, one byte is equal to one character. So you would read the first 20 bytes to get the first 20 characters of the file. The second technique for reading a file is the PHP function file_get_contents. It reads an entire file all at once. Notice that unlike fread, it takes a file path as an argument instead of a file handle. It's like fopen and that's because file_get_contents is a shortcut for opening a file, reading the entire contents and then closing it. Let's try them out. Let's begin by doing a select all and copy of everything that's in…
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)
-
-
-