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.
- Read & Return Data From Files Into PHP
- pandas read data from files
- C++ Read Data from File into Struct
- Use pandas to read data from files
- lessons learned from the Qlikview loads the data from DB into qvd files
- Dump data into MySQL from Excel with PHP
- Read Excel files from Python
- Import Data from Txt or CSV files into MYSQL database tables
- Loading data from .plist files
- Read CSV From PHP
- Android: read files from assets in NDK
- Insight into DOMDocument - how to convert data from XML to array in PHP
- Read and Write files IN PHP
- Read pixel data from bitmap file
- 3. Servlet read data from forms
- How to parse / read JSON data into a Android ListView
- write to and read from files (读写文件)
- convert the Data from Table into Insert Sentence.
- S2SH 整合 主要关注struct2
- 在DC操作中SelectObject后要注意释放内存
- C++模板中type_traits(类型特化)
- 迁移老的博客文章
- C#日期格式
- Read & Return Data From Files Into PHP
- hdu 1174 爆头
- zoj 2851 Code Formatter 水题(1)
- CMake的使用方法_小结
- static_cast、dynamic_cast、reinterpret_cast、和const_c
- ace第二课-- 一个日志服务器的实现
- 网络爬虫,,,spider
- 更新时间戳的命令
- Android 用axis2 webService产生java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive错误的解决