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

Reading from files - PHP Tutorial

From the course: PHP Techniques: Working with Files and Directories

Start my 1-month free trial

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