![]() |
|
| PHP Tutorial | PHP Resources | PHP Server | |
Imagine that you are about to access a file in your server which contains a list of email address called 'data.txt'. hello@example.com
john@example.com david@example.com php@example.com Before you can read or edit a file, you ought to establish a connection to it. This connection is done by using fopen( ) function. <?php
?> This fopen( ) function takes two argument. First, you have to specify the name of the file. In this case, I'm inheriting value from $file. Second argument is the access mode. The word 'r' represents 'read'. When you open a connection, either it is database connection or file connection, you have to close it after PHP finish it's job. In file connection, it use fclose( ) function to close file handler. And every code that will read those data within 'data.txt' should be located between fopen( ) and fclose( ). In the above example, it still has no code yet! Now we are going to read the file. <?php
?> Notice that !feof in the while statement. It tells PHP not to stop reading the file until it reach the end of the page or when it had reach it's length limit defined in fgets( ) function. fgets( ) is a function provided by PHP to get lines from file pointer. It takes two argument. One is the file pointer, in this case, our file pointer is $fp. And the second one is length limit. Thus, when the code above is executed, it will print out the result below. hello@example.com
john@example.com david@example.com php@example.com There is another easy way to read files in PHP. You can use file( ) function to do this. File( ) function will automatically establish a connection to file, read the content and close it when it has finished. But the result is stored in an array. Still taking the above example, we are going to print out the same result using file( ) function. Fast and easy! <?php
?> |
Optimize Website - Webmasters optimize their site and gain higher search engine ranking Free Photo Hosting - Free File Hosting, Free ZIP file Hosting, Free Image Hosting, Free MP3 Hosting. Stupid Videos - Custom Pimped Motorcycle Pictures, Videos, and Articles Forectory - Webmaster Directory St Louis Computer NetworkingWebsite Templates - 1000s of Templates $5 - Many Templates to help you build your site!
|
Each and every tutorial published in PHPhelps.com
is originally written by PHPHELPS OWNERS. None of the content is allowed
to be replicated without prior notice to writter. Copyright © 2006 PHPhelps.com |