Read & Return Data From Files Into PHP

来源:互联网 发布:知乎 男友阴茎太大 编辑:程序博客网 时间:2024/05/26 22:09

Reading File Data

If you have written data to a file from PHP, or you have a file on your server you need to read with PHP, there are quite a few ways you can do it. The four ways we will cover here arefread (),fgets (),file (), andfile_get_contents(). The one you choose is based upon your needs, and how the data in the file you are accessing is formatted.


Using fread () in PHP

You can use fread () when you want to read the file data and return it to a string. You can specify how many bytes to read, but 8192 is the maximum. If you choose to use this method, you must be sure toopen the file first.

 <?php 

 $YourFile = "YourFile.txt"; 

 $handle = fopen($YourFile, 'r'); 

 $Data = fread($handle, 512); 

 fclose($handle); 

 print $Data; 

 ?> 

This code opens the file YourFile.txt from your server and puts it's entire contents into the variable $Data as a string. We then print $Data, so the script is basically outputting the file contents onto a page.


Using file_get_contents () in PHP

File_get_contents () is similar tofread () in that it returns the entire contents of the file to a single string, however it can be done in a single line and performs better thenfread ().

 <?php 

 $file = file_get_contents ('YourFile.txt'); 

 Echo $file; 

 ?> 

This little bit of code retrieves the data from YourFile.text and thenechos it onto the page.


Using fgets () in PHP

Fgets () is used to read the data from a file one line at a time starting from the pointer. It defaults to only reading the first 1024 bytes of a line, however you can set this variable higher or lower if you wish. If your file is not separated with line breaks, this is not the right function to use.

 <?php 

 $YourFile = "YourFile.txt"; 

 $handle = fopen($YourFile, 'r'); 

 while (!feof($handle)) 

 { 

 $Data = fgets($handle, 256); 

 print $Data; 

 print "<p>"; 

 } 

 fclose($handle); 

 ?> 

What this code does first is open the file YourFile.txt. It then enters into a loop that will read up to 256 bytes of the file line, print the contents of the line, print an HTML paragraph break, and then repeat this with the next line until it reaches the end of the file (foef).


Using file () in PHP

File () is similar tofgets in that it reads the data one line at a time, however it returns it all at once into an array. The array contains the line number, and the data corresponding to each line number starting with 0. If you want to do more than simplyecho the data, having it in anarray will provide much more flexibility makingfile () more useful than fgets ().

 <?php 

 $lines = file('YourFile.txt'); 

 foreach ($lines as $line_num => $line) 

 { 

 print "<font color=red>Line #{$line_num}</font> : " . $line . "<br />\n"; 

 }

 ?> 

What this code does first is put the contents of YourFile.txt into the array called $lines. It then enters into a loop which prints each line number in red, followed by the line contents, and repeats until all lines have been printed.