PHP working with files
Last update on: 10-08-2008You may need to work with text files to store various information for your site, this course will serve to better understand the various uses of functions.
The functions to use to open files are:
file(); //Browse a file and put it in an array fopen(); //open a file
The add-ons for these functions are:
'w' : open a file to write, and erase everything was before.
'w+' : the same thing as (W) but this one create the file if doesn't exist.
'a' : open a file to add data.
'a+' : the same thing as (a) but this one create the file if doesn't exist.
'r' : open a file for reading only.
'r+' :open a file for reading and writing.
Read the content of a file:
// we declare the name of the file $myfile = 'myfile1.txt'; //we count the size of the file $its_size = filesize($myfile); //open the file for read only $fp = fopen($myfile,'r'); //feof shows the end of the file... //So it reading the file until the end. while( !feof($fp) ) { //reading the file and putting it on the variable line. $line = fgets($fp,$taille); //display the line affiche la line.. /Do not forget to put the <br> for the next line. print $ligne.'<br />'; } //we close at the end fclose($fp);
Note: To read the content, you can use the fread() function it does the same work as fgets().
Write on a file:
we will use the same example but we will open the file this time to write.// we declare the name of the file $myfile = 'myfile1.txt'; //open the file to write and delete the data that was on it before $fp = fopen($fichier,'w'); //we write on the file, the \n tells the function to move to another line. fwrite($fp,"my first line \n my second line"); //at the end we close the file fclose($fp);
Note: To write the content, you can use the fputs() function it does the same work as fwrite().
Do it yourself
Now we will write on a file but we need to keep the data that the file has already.
What we can do with this lesson.
Example: mini visitors counter :-)
//declare the file $myfile = 'counter.txt'; // here we use the file() function that puts the file content... // as an array on the $fp variable. $fp = file($myfile); //because it's a counter, we will read just the first line on the file... // where the number is $fp[0]. // For each new visit, we add 1 to the actual number. $counterr = $fp['0'] + 1; //open the file to write. $new = fopen($myfile,'w+'); //delete the old content and write the new number on the file, fwrite($new,"$counter \n"); //close the file fclose($new);
We can limit the time to give by each visitor to be added as a visitor by using cookies, like that each view page will not counted as a visit.
This is just an example just to give you an idea .
This is just an example just to give you an idea .
PHP and MySQL's lessons:
Introduction To PHPGet Started With PHP
PHP Variables
PHP Variables Of Environment
PHP Conditions
PHP Looping
PHP Cookies
PHP Working With Dates
PHP Working With Arrays
PHP Working With Files
PHP Play With Strings
PHP And Forms
Send Emails With PHP
The Include Statement
Get Started With MySQL
MySQL Update And Delete
The WHERE Clause
MySQL Functions
Guestbook Script
Websites Directory Script
Multiple Pages With PHP
Create Your Forum With Php

