Read & Return Data From Files Into PHP

本文详细介绍了PHP中用于读取文件的四种方法:fread()、fgets()、file()和file_get_contents(),并提供了示例代码演示如何使用这些方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 are fread (),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 to open 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 then echos 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.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值