Showing posts with label read. Show all posts
Showing posts with label read. Show all posts

Tuesday, June 7, 2016

PHP File Read

My apologies for taking so long to actually get to the point where you get information from files. In this lesson we will teach you how to read data from a file using various PHP functions.

PHP - File Open: Read

Before we can read information from a file we have to use the function fopen to open the file for reading. Heres the code to read-open the file we created in the PHP File Write lessons.

PHP Code:

$myFile = "testFile.txt";
$fh = fopen($myFile, r);

The file we created in the last lesson was named "testFile.txt". Your PHP script that you are writing should reside in the same directory as "text.txt". Here are the contents of our file from File Write.

testFile.txt Contents:

Floppy Jalopy Pointy Pinto

Now that the file is open, with read permissions enabled, we can get started!

PHP - File Read: fread Function

The fread function is the staple for getting data out of a file. The function requires a file handle, which we have, and an integer to tell the function how much data, in bytes, it is supposed to read.
One character is equal to one byte. If you wanted to read the first five characters then you would use five as the integer.

PHP Code:

$myFile = "testFile.txt";
$fh = fopen($myFile, r);
$theData = fread($fh, 5);
fclose($fh);
echo $theData;

Display:

Flopp

The first five characters from the testFile.txt file are now stored inside $theData. You could echo this string, $theData, or write it to another file.

If you wanted to read all the data from the file, then you need to get the size of the file. The filesize function returns the length of a file, in bytes, which is just what we need! The filesize function requires the name of the file that is to be sized up.

PHP Code:

$myFile = "testFile.txt";
$fh = fopen($myFile, r);
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;

Display:

Floppy Jalopy Pointy Pinto

Note: It is all on one line because our "testFile.txt" file did not have a
tag to create an HTML line break. Now the entire contents of the testFile.txt file is stored in the string variable $theData.


PHP - File Read: gets Function

PHP also lets you read a line of data at a time from a file with the gets function. This can or cannot be useful to you, the programmer. If you had separated your data with new lines then you could read in one segment of data at a time with the gets function.

Lucky for us our "testFile.txt" file is separated by new lines and we can utilize this function.

PHP Code:

$myFile = "testFile.txt";
$fh = fopen($myFile, r);
$theData = fgets($fh);
fclose($fh);
echo $theData;

testFile.txt Contents:

Floppy Jalopy

The fgets function searches for the first occurrence of " " the newline character. If you did not write newline characters to your file as we have done in File Write, then this function might not work the way you expect it to.

Tuesday, January 26, 2016

Ramadan Mubarak 2015 9 Bangla Islamic Books To Read Learn Islam

The month of fasting has come at the door and for the Muslim, It is the month of maximizing praying. Yes, maximizing praying....Because, in this month, if you pray for one time, You will be benefited for 70 times. So, in this month, A Muslim should pray to Allah regularly and if he or she can pray more, then he or she should pray more because of getting 70 extra benefits in case of regular.

In this month, "Dujokh" remains close and "Jannat" remains open. All Muslim become busy in "Siam Sadhona". So, If you are a Muslim and think that you make this month as useful and full of praying, Then, here are some Islamic books which may guide you well in the case of providing useful knowledge about Islam. These Bangla Islamic books are awesome and great to follow. 





     04. Iman Sobar Age



     05. Imaner Durbolota



     08. Amol O Jikir


So, download all of these Bangla  books i.e. Bangla Islamic Books and learn or enrich your Islamic knowledge to make your mind direction toward Allah and Islam so that you can be benefited on the earth and Akhirat. Pray for us too. :)