Sunday, June 30, 2013

php fopen and how to use it

php fopen function and how to use it with files
In this lesson we will talk about how to deal with files in php, first let's create a text file and name it "text.txt", write whatever you want in it 

php fopen

After that we will use php fopen like this 
<?php
$file=fopen("text.txt",'r');
?>
The fopen function have two parameter
fopen("first parameter","second parameter") 
first parameter: we put the name of the file or the link of it

second parameter: we put the way that we want to use to open the file
There is many ways to open a file
"r": to open a file for only reading start at the begining of the file
"w": to open a file and write in it at the beginning or create a new file if its not exist, this method will erase all the data in the file
"a": to open a file and write in it at the end of it or create a new file if it's not exist
"r+": to read or write in the file start at the begining of the file
"w+": to read or write in the file with erase all the data or create a new file if it's not exist
"a+": to read or write in the file with keeping all the data in that file start at the end of the file

Let's continue the last example
If we write the last example like this
<?php
$file=fopen("text.txt",'r');
echo "$file";
?>
We'll see this on the screen
php fopen
But we should have Hi how are you! because this is what we wrote in the file, so how?
to print the content of the file we must write this code using php fgets function

<?php
$file = fopen("text.txt", "r");
$text=fgets($file);
echo "$text";
fclose($file);
?>
And the result is
php fopen
As you see we use three functions (fopen,fgets,fclose)
In the first line we asking PHP to open up the file and in the second line we use the fgets function and what it does is to get the a line of the file, and we store it in the text variable $text 
After that we print the variable $text and close the file with the function fclose

I think you are wondering now if we have a file with two or three lines how we can print theme all in the screen, well there are loop's in PHP and we will use while loop to solve this problem keep reading and you will know how
<?php
$file = fopen("text.txt", "r");
while (!feof($file)) {
$line = fgets($file);
print $line . "<BR>";
}
fclose($file);
?>

Let's explaine the code 
we asking PHP to open the file 
we put a loop and a condition in it !feof($file)  feof is a function that tells PHP when the end of a file has been reached but we use the not ! operator 
so this code while (!feof($file)) is like we said while the end of the file has been not reached keep looping
So we get a line of text from our file, and then place the line into a variable. We then print out the line of text
print $line . "<BR>"; this line is to printing out the line of text, we're adding a HTML line break
fclose($file); After the loop is finished we close the file

This was the php fopen and other functions, I hope you learn something today please share this lesson with your friends if you like it
See you next time.

Share This!



No comments :

Post a Comment