If you've used C before, you're probably already familiar with the "include" directive that appears near the beginning of every C program. Since we're talking about reading files, it's appropriate to bring in PHP's equivalent - the include() and require() functions, which come in handy when you need to read an external file into your PHP script. Consider the following simple example, which illustrates:
Now, if you try to access this page as it, you'll get a bunch of error messages warning you about missing files. So you need to create the files "gun.php4", "car.php4" and "watch.php4":
And this time, when you access the primary page, PHP should automatically include the specified files, read the variables $gun, $watch and $car from them, and display them on the page. Files named in the include() and require() functions are searched for in a set of default locations. These locations are specified via PHP's "include_path" configuration directive, which may be set globally in the main "php.ini" configuration file, or on a per-script basis using the A quick note on the difference between the include() and require() functions - the require() function returns a fatal error if the named file cannot be found and halts script processing, while the include() function returns a warning but allows script processing to continue. An important point to be noted is that when a file is require()-d or include()-d, the PHP parser leaves "PHP mode" and goes back to regular "HTML mode". Therefore, all PHP code within the included external files needs to be enclosed within regular PHP <?...?> tags. A very useful and practical application of the include() function is to use it to include a standard footer or copyright notice across all the pages of your Web site, like this:
where "footer.html" contains
Now, this footer will appear on each and every page that contains the Note also that PHP also offers the require_once() and include_once() functions, which ensure that a file which has already been read is not read again. This can come in handy if you have a situation in which you want to eliminate multiple reads of the same include file, either for performance reasons or to avoid corruption of the variable space. {mospagebreak title=A Little Brainwashing}Obviously, reading a file is no great shakes - but how about writing to a file? Well, this next script does just that:
Now, once you run this script, it should create a file named "matrix.txt", which contains the text above. As you can see, in order to open a file for writing, you simply need to use the same fopen() function, but with a different mode ("w" for write).
A number of different modes are available for the fopen() function - you've already seen two, here are a few more:
w Opens a file in write mode; existing file contents are truncated a Opens a file in append mode; existing file contents are preserved You can add a "+" to any of the modes above to open the file for simultaneous read and write. In case the named file does not exist in any of the write modes, a new file with the specified name is created. Once the file has been opened, you can write text to it simply by specifying the data to be written as an argument to the fwrite() function.
Close the file, and you're done! You can also write to a file with the new file_put_contents() function, which accepts a string as input and writes it to the named file with minimal fuss or muss.
blog comments powered by Disqus |
|
|
|
|
|
|
|