Working with Files in PHP
See all Sams Teach Yourself on InformIT Web Development Tutorials.
Testing, reading, and writing to files are staple activities for any full-featured programming language. PHP is no exception, providing you with functions that make the process straightforward. In this hour, you will learn
How to include files in your documents
How to test files and directories
How to open a file before working with it
How to read data from files
How to write or append to a file
How to lock a file
How to work with directories
Including Files with include()
The include() statement enables you to incorporate files into your PHP documents. PHP code in these files can be executed as if it were part of the main document. This can be useful for including library code in multiple pages.
Having created a killer function, your only option until now would have been to paste it into every document that needs to use it. Of course, if you discover a bug, or want to add a feature, you will have to find every page that uses the function to make the change. The include() statement can save you from this chore. You can add the function to a single document and, at runtime, read this into any page that needs it. The include() statement requires a single argument, a relative path to the file to be included. Listing 10.1 creates a simple PHP script that uses include() to incorporate and output the contents of a file.
Listing 10.1 Using include()
1: <html> 2: <head> 3: <title>Listing 10.1 Using include()</title> 4: </head> 5: <body> 6: <?php 7: include("listing10.2.php"); 8: ?> 9: </body> 10: </html>
The include() statement in Listing 10.1 incorporates the document listing10.2.php, the contents of which you can see in Listing 10.2. When run, Listing 10.1 outputs the string "I have been included!!", which may seem strange, given that we have included plain text within a block of PHP code. Shouldn't this cause an error? In fact, the contents of an included file are displayed as HTML by default. If you want to execute PHP code in an included file, you must enclose it in PHP start and end tags. In Listings 10.3 and 10.4, we amend the previous example so that code is executed in the included file.
Listing 10.2 The File Included in Listing 10.1
1: I have been included!!
Listing 10.3 Using the include() statement to Execute PHP in Another File
1: <html> 2: <head> 3: <title>Listing 10.3 Using include to execute PHP in another file</title> 4: </head> 5: <body> 6: <?php 7: include("listing10.4.php"); 8: ?> 9: </body> 10: </html>
Listing 10.4 An Include File Containing PHP Code
1: <?php 2: print "I have been included!!<BR>"; 3: print "But now I can add up... 4 + 4 = ".(4 + 4); 4: ?>
Returning a Value From an Included Document
Included files in PHP4 can return a value in the same way as functions do. As in a function, using the return statement ends the execution of code within the included file. Additionally, no further HTML will be included. In Listings 10.5 and 10.6, we include a file, assigning its return value to a variable.
Listing 10.5 Using include() to Execute PHP and Assign the Return Value
1: <html> 2: <head> 3: <title>Listing 10.5 Using include() to execute PHP and assign the return value</title> 4: </head> 5: <body> 6: <?php 7: $addResult = include("listing10.6.php"); 8: print "The include file returned $addResult"; 9: ?> 10: </body> 11: </html>
Listing 10.6 An Include File that Returns a Value
1: <?php 2: $retval = ( 4 + 4 ); 3: return $retval; 4: ?> 5: This HTML should never be displayed because it comes after a return statement!
NOTE
Returning values from included files would only work in PHP3 if the return statement was contained in a function. The code in Listing 10.6 would cause an error.
Using include() Within Control Structures
You can use an include() statement in a conditional statement, and the referenced file will only be read if the condition is met. The include() statement in the following fragment will never be called, for example
$test = false; if ( $test ) { include( "a_file.txt" ); // won't be included }
If you use an include() statement within a loop, it will be replaced with the contents of the referenced file each time the include() statement is called. This content will be executed for every call. Listing 10.7 illustrates this by using an include() statement in a for loop. The include() statement references a different file for each iteration.
Listing 10.7 Using include() Within a Loop
1: <html> 2: <head> 3: <title>Listing 10.7 Using include() within a loop</title> 4: </head> 5: <body> 6: <?php 7: for ( $x = 1; $x<=3; $x++ ) { 8: $incfile = "incfile$x".".txt"; 9: print "Attempting include $incfile<br>"; 10: include( "$incfile" ); 11: print "<p>"; 12: } 13: ?> 14: </body> 15: </html>
When Listing 10.7 is run, it includes the content of three different files, "incfile1.txt", "incfile2.txt", and "incfile3.txt". Assuming that each of these files simply contains a confirmation of its own name, the output should look something like this:
Attempting include incfile1.txt This is incfile1.txt Attempting include incfile2.txt This is incfile2.txt Attempting include incfile3.txt This is incfile3.txt