How to Read a File Line by Line in PHP
PHP is a scripting language created in 1995 by Rasmus Lerdorf. It was originally designed to create dynamic web pages, but it can also be used in a standalone graphical interface. The PHP Group manages the specifications and keeps the coding standards for PHP coding. A line-by-line reading can be used for many purposes. Search engines go through documents and web sites line by line to grab keywords to enable users' searches. By reading a document line by line, the programmer can then add code to pull out specific words and utilize them for various tasks.
Instructions
-
-
1
Open a text editor, such as Notepad or Wordpad. (Microsoft Word adds a lot of additional formatting and can create problems with the code.)
-
2
Type "<?PHP" without the quotes. This tells the browser that the following code is in the PHP format.
-
-
3
Type "$GetFile = @fopen("/tmp/filetoread.txt","r");" without the end quotes. $GetFile (GetFile can be named whatever you want--just keep the $ in front of it) is the string to which you are assigning the file. @fopen is the function to open the file you are going to read. /temp/filetoread.txt is the location of the file and file name you will open. "r" is telling the code to open the file for reading purposes. Right now, your code should look like this:
<?PHP
$GetFile = @fopen("/tmp/filetoread.txt","r"); -
4
Type "if($GetFile){" without the quotes. The "If" statement is for conditional usage. ($GetFile) is the name of the string we are calling, which in this case carries the file we want to read. The code within the {} will be the conditions set forth in the IF statement. Your code now looks like this:
<?PHP
$GetFile = @fopen("/tmp/filetoread.txt","r");
if($GetFile){ -
5
Type "while (!eof($GetFile)){" without the quotes. "While" is a loop, and eof stands for "end of file." This line tells the code to continue looping until you reach the end of the file. What it is doing while it is looping is in the next step. You code looks like this:
<?PHP
$GetFile = @fopen("/tmp/filetoread.txt","r");
if($GetFile){
while (!eof($handle)){ -
6
Type "$StoreInfo = fgets($GetFile,4096);" without the quotes. $StoreInfo is another string that will be storing information. In this case, fgets grabs a line from the text file and dumps into $StoreInfo. Code looks like this:
<?PHP
$GetFile = @fopen("/tmp/filetoread.txt","r");
if($GetFile){
while (!eof($handle)){
$StoreInfo = fgets($GetFile,4096); -
7
Type "echo $StoreInfo;" without the quotes. The echo command prints the information stored in the $StoreInfo string. Your code looks like this:
<?PHP
$GetFile = @fopen("/tmp/filetoread.txt","r");
if($GetFile){
while (!eof($handle)){
$StoreInfo = fgets($GetFile,4096);
echo $StoreInfo; -
8
Type "}" without the quotes. This little character ends the looping process. When the code has gone through the entire file and printed every line, you are finished. Your code looks like this:
<?PHP
$GetFile = @fopen("/tmp/filetoread.txt","r");
if($GetFile){
while (!eof($handle)){
$StoreInfo = fgets($GetFile,4096);
echo $StoreInfo;
} -
9
Type "fclose($GetFile)"; without the quotes. This command closes the original document from which you were getting the information. Your code looks like this:
<?PHP
$GetFile = @fopen("/tmp/filetoread.txt","r");
if($GetFile){
while (!eof($handle)){
$StoreInfo = fgets($GetFile,4096);
echo $StoreInfo;
}
fclose($GetFile); -
10
Type "}" without the quotes. This closes the IF statement you were using. Your code looks like this:
<?PHP
$GetFile = @fopen("/tmp/filetoread.txt","r");
if($GetFile){
while (!eof($handle)){
$StoreInfo = fgets($GetFile,4096);
echo $StoreInfo;
}
fclose($GetFile);
} -
11
Type "?>" without the quotes. This closes your PHP session. Your final code looks like this:
<?PHP
$GetFile = @fopen("/tmp/filetoread.txt","r");
if($GetFile){
while (!eof($handle)){
$StoreInfo = fgets($GetFile,4096);
echo $StoreInfo;
}
fclose($GetFile);
}
?> -
12
Save the file with a .php extension and upload it to your server.
-
1
Tips & Warnings
You should have at least a moderate amount of coding experience, and an understanding of how websites handle coding, to properly work with PHP. You must have knowledge of how to ftp the .php file to your server to be able to use it.
PHP is typically installed on a server. You cannot run PHP on your desktop without having installed the PHP server program.
References
Resources
Comments
-
frosty555
Feb 11, 2011
The horrific coding standards of this article aside, it would be nice if the code actually worked, which it doesn't. This article has TWO different syntax errors that will stop the code from running. The while loop references variable $handle, which doesn't exist (should have been $GetFile), and also there is no such function "eof()" in PHP. Did you perhaps mean feof()? Seriously nobody even bothered trying to compile or run this code. This article is embarassing.