Home > Tutorials > PHP > Using Other Files

Using Other Files

Reuseability is always the way to go - if there's something you want to use multiple times, don't copy/paste - write it once in a PHP file, or read it multiple times from an HTML file!

Include/Require

You are going to want to use include() and require() to access all of your common functions. They both do the same thing (that is, reference a given filename, much like an other programming language), the only difference between the two is that include() will generate a warning if the specified file can't be found, and require() will generate a fatal error (i.e., the script will stop right there).

File I/O

Reading files from HTML pages hosted on the server can make things much easier when it comes to editing. Your header and footer are two sections of your website which won't depend on which specific webpage you are on (that is, they are the same on your homepage, about page, etc.). So, when you want to edit these (and other repeated elements of your page), why bother editing them in multiple locations? Just place them in their own separate files, then read from them when the time comes. Here's the code to use: $file = fopen('header.html', "r"); $contents = fread($file, filesize('header.html')); fclose($file); echo $contents; fopen opens the specified file, 'header.html'. The 'r' parameter says we want to read this file (as opposed to write or append - we'll get to those later). To actually read the file, we will need to specify (besides the file) how much data (in bytes) can be read at a time. Since we may not know the size of the file (and since we might edit the file later on), we just use the filesize function.

To write, instead of passing in 'r', pass in 'w', and instead of using fread, use the fwrite(filename,<string to write>) command. Appending (opening a file for both reading AND writing) can be used by passing in 'a' to the fread command.

There are, of course, many more built in functions to work with files. Here is a short list of some of them:

FunctionUsageExample
feof(file)Checks for end of file$file=fopen('header.html'); while (!feof($file)) { $content=fread($file,1024); echo $content; } fclose($file);
fgetc($file)Reads one character from $file$file=fopen('header.html'); while (!feof()) { $char=fgetc($file); echo $char; } fclose($file);
fgets($file,$bytes)Reads one line from $file. If $bytes is specified, a maximum number of bytes will be read (default is 1024).$file=fopen('header.html'); while (!feof($file)) { $line=fgets($file); echo $line; } fclose($file);
file($file)An alternate to fopen and fread, reads $file into an array, line by line.$file=file('header.html'); foreach ($line in $file){ echo $file[$line]; }
file_exists($file)Tests for whether or not $file exists.if (!file_exists('header.html')){ echo "We're in trouble - we can't find the header!!"; }

For the complete set of File I/O functions, see the PHP manuaul at www.php.net.

<<PreviousNext>>

Comment on this page:

Your information Name:
Email:
Hide Email?
Enter the 5 characters shown.
Request another image.
Prove your human:
Comments Overall page rating:

Comments

No one has commented on this lesson yet. You can be the first!