初学PHP的时候使用了一些文件读取API,但是没有真正弄清楚各API的区别以及差异,于是找了一篇学习了一下,贴在这里,引用自IBM社区的一篇文章

整体整理测试如下

<?php
/**
* Created by PhpStorm.
* User: f3ngt1ng
* Date: 2017/2/15
* Time: 9:11
*/ //The right way to read files with php
//URL:https://www.ibm.com/developerworks/library/os-php-readfiles/ /**
* feof
* The feof command detects whether you have already read to the end of
* the file and returns True or False. The loop in Listing 1 continues until
* you have reached the end of the file "myfile." Note that feof also returns False
* if you're reading a URL and the socket has timed out because you no longer have
* data to read.
*/
function One(){
$path = '../2.14/filter.txt';
$handler = fopen($path, 'r');
while(!feof($handler)){
/**
*Hearkening back to the "\0" end-of-string terminator in C,
* set the length to one number higher than you actually want.
* Thus, the example above uses 81 when you want 80 characters.
* Get in the habit of remembering to add that extra character
* whenever you use the line limit on this function.
*/
$line = fgets($handler, 3);
echo $line."\r\n";
}
fclose($handler);
} //One();
function Two(){
/* fread($handler, 4096);
* This is where fread comes in.
* The fread function serves a slightly different purpose from fgets:
* It is intended to read from binary files (that is,
* files that don't consist primarily of human-readable text).
* Because the concept of "lines" isn't relevant for binary files
* (logical data constructs are not generally terminated by newlines),
* you must always specify the number of bytes that you wish to read in.
*
* The above reads in 4,096 bytes (4 KB) of data.
* Note that no matter what number you specify,
* fread will not read more than 8,192 bytes (8 KB).
* Assuming that the file is no bigger than 8 KB,
* the code below should read the entire file into a string.
*
*
* If the file is longer than this, you will have to use a loop
* to read the rest in.
*/ } function Three(){
/* fscanf
*list ($field1, $field2, $field3) = fscanf($fh, "%s %s %s");
* example:
* $handle = fopen("users.txt", "r");
* while ($userinfo = fscanf($handle, "%s\t%s\t%s\n")) {
* list ($name, $profession, $countrycode) = $userinfo;
* //... do something with the values
* }
* fclose($handle);
*
* Coming back to string processing,
* fscanf again follows the traditional C file library functions.
* If you're unfamiliar with it, fscanf reads field data into variables
* from a file.
*/
} function Four(){
/*
* fgetss
* The fgetss function breaks away from the traditional file
* functions and gives you a better idea of the power of PHP.
* The function acts like fgets, but strips away any HTML or
* PHP tags it finds, leaving only naked text. Take the HTML file shown below.
*/ } function Five(){
/*
* fpassthru
* No matter how you've been reading your file,
* you can dump the rest of your data to your standard output channel
* using fpassthru.
* Again, this function prints the data,
* so you don't need to grab the data in a variable.
*/
$path = '../2.14/filter.txt';
$handler = fopen($path, 'r');
$line = fgets($handler, 2);
echo $line."==========\r\n";
fpassthru($handler); }
//Five(); function Six(){
//Nonlinear file processing :Jumping Around
/*
* Of course, the above functions only allow you to
* read a file in order. More complex files might require
* you to jump back and forth to different parts of the file.
* This is where fseek comes in handy.
* fseek($handler, 0)
*
* From PHP V4.0 on, you have a few other options.
* For example, if you want to jump ahead 100 bytes from your
* current position, you can try:
*
* fseek($fh, 100, SEEK_CUR);
* Similarly, you can jump back 100 bytes by using:
* fseek($fh, -100, SEEK_CUR);
* If you want to jump back 100 bytes before the end of
* the file, use SEEK_END, instead.
*
* fseek($fh, -100, SEEK_END);
*
* After you've reached the new position, you can use fgets,
* fscanf, or anything else to read the data.
* Note: You can't use fseek on file handles referring to URLs.
*/ } function Seven(){
/*
* Now we get to some of PHP's more unique file-processing strengths:
* dealing with massive chunks of data in a line or two. For example,
* how might you grab a file and display the entire contents on your Web page?
* Well, you saw an example using a loop with fgets. But how can you make
* this more straightforward? The process is almost ridiculously easy with
* fgetcontents, which places an entire file within a string.
* echo file_get_contents("myfilename");
* This article is primarily about dealing with local files,
* but it's worth noting that you can grab, echo, and parse other Web pages
* with these functions, as well.
*
* echo file_get_contents("http://127.0.0.1/");
* This command is effectively the same as:
* $fh = fopen("http://127.0.0.1/", "r");
* fpassthru($fh);
*
* readfile("http://127.0.0.1/");
* The readfile function dumps the entire contents of a file or Web page
* to the default output buffer. By default, this command prints an error message
* if it fails. To avoid this behavior (if you want to), try:
* @readfile("http://127.0.0.1/");
*/ } function Eight(){
/*
* Of course, if you actually want to parse your files,
* the single string that file_get_contents returns might be a bit overwhelming.
* Your first inclination might be to break it up a little bit with the split()
* function.
* $array = split("\n", file_get_contents("myfile"));
*
* But why go through all that trouble when there's a perfectly
* good function to do it for you? PHP's file() function does this in one step:
* It returns an array of strings broken up by lines.
*
* $array = file("myfile");
* It should be noted that there is a slight difference between the above two
* examples. While the split command drops the newlines, the newlines are
* still attached to the strings in the array when using the file command
* (as with the fgets command).
*/
} function Nine(){
/*
* PHP's power goes far beyond this, though.
* You can parse entire PHP-style .ini files in a single command using
* parse_ini_file. The parse_ini_file command accepts files similar
* to Listing 4.
*
* ; Comment
* [personal information]
* name = "King Arthur"
* quest = To seek the holy grail
* favorite color = Blue * [more stuff]
* Samuel Clemens = Mark Twain
* Caryn Johnson = Whoopi Goldberg
*
* $file_array = parse_ini_file("holy_grail.ini");
* print_r $file_array;
*
* OUTPUT:
* Array
* (
* [name] => King Arthur
* [quest] => To seek the Holy Grail
* [favorite color] => Blue
* [Samuel Clemens] => Mark Twain
* [Caryn Johnson] => Whoopi Goldberg
* )
*
* Of course, you might notice that this command merged the sections.
* This is the default behavior, but you can fix it easily by passing a second
* argument to parse_ini_file: process_sections, which is a Boolean variable.
* Set process_sections to True.
*
* $file_array = parse_ini_file("holy_grail.ini", true);
* print_r $file_array;
*
* Array
* (
* [personal information] => Array
* (
* [name] => King Arthur
* [quest] => To seek the Holy Grail
* [favorite color] => Blue
* )
* [more stuff] => Array
* (
* [Samuel Clemens] => Mark Twain
* [Caryn Johnson] => Whoopi Goldberg
* ) * )
*/ }

关于php文件读取的一些学习记录的更多相关文章

  1. Python编码/文件读取/多线程

    Python编码/文件读取/多线程 个人笔记~~记录才有成长   编码/文件读取/多线程 编码 常用的一般是gbk.utf-8,而在python中字符串一般是用Unicode来操作,这样才能按照单个字 ...

  2. leveldb 学习记录(四)Log文件

    前文记录 leveldb 学习记录(一) skiplistleveldb 学习记录(二) Sliceleveldb 学习记录(三) MemTable 与 Immutable Memtablelevel ...

  3. git 学习记录—— git 中的仓库、文件状态、修改和提交操作等

    最近开始学习使用版本控制工具  git .学习方式主要通过阅读 git 网站上的 Pro git 和动手实践,使用的系统为 Ubuntu16.04LTS,以及 Windows 8.1. 本文主要关注 ...

  4. 深度学习_1_Tensorflow_2_数据_文件读取

    tensorflow 数据读取 队列和线程 文件读取, 图片处理 问题:大文件读取,读取速度, 在tensorflow中真正的多线程 子线程读取数据 向队列放数据(如每次100个),主线程学习,不用全 ...

  5. 风炫安全web安全学习第三十五节课 文件下载和文件读取漏洞

    风炫安全web安全学习第三十五节课 文件下载和文件读取漏洞 0x03 任意文件下载漏洞 一些网站由于业务需求,往往需要提供文件下载功能,但若对用户下载的文件不做限制,则恶意用户就能够下载任意敏感文件, ...

  6. Java学习-019-Properties 文件读取实例源代码

    在这几天的学习过程中,有开发的朋友告知我,每个编程语言基本都有相应的配置文件支持类,像 Python 编程语言中支持的 ini 文件及其对应的配置文件读取类 ConfigParse,通过这个类,用户可 ...

  7. Java学习-017-EXCEL 文件读取实例源代码

    众所周知,EXCEL 也是软件测试开发过程中,常用的数据文件导入导出时的类型文件之一,此文主要讲述如何通过 EXCEL 文件中 Sheet 的索引(index)或者 Sheet 名称获取文件中对应 S ...

  8. Java学习-016-CSV 文件读取实例源代码

    上文(CSV文件写入)讲述了日常自动化测试过程中将测试数据写入 CSV 文件的源码,此文主要讲述如何从 CSV 文件获取测试过程中所需的参数化数据.敬请各位小主参阅,若有不足之处,敬请大神指正,不胜感 ...

  9. Java学习-013-文本文件读取实例源代码(两种数据返回格式)

    此文源码主要为应用 Java 读取文本文件内容实例的源代码.若有不足之处,敬请大神指正,不胜感激! 1.读取的文本文件内容以一维数组[LinkedList<String>]的形式返回,源代 ...

随机推荐

  1. UltraEdit MAC破解方法

    在终端输入 printf '\x31\xC0\xFF\xC0\xC3\x90' | dd seek=$((0x92D370)) conv=notrunc bs=1 of=/Applications/U ...

  2. jfinal框架新手使用之路及开发心得

    从接触jfinal这个框架到现在差不多也有一个的时间了,因为之前接触的都是像spring ,springMVC,mybatis,struts2,hibernate这种传统,大多数公司都在用的这种框架. ...

  3. 移动端JS事件、移动端框架

    一.移动端的操作方式和PC端是不同的,移动端主要是用手指操作,所以有特殊的touch事件,touch事件包括如下几个事件: 1.手指放到屏幕上时触发   touchstart 2.手指放在屏幕上滑动式 ...

  4. Java之集合初探(一)

    一.集合概述.区别 集合是一种容器,数组也是一种容器 在Java编程中,装各种各样的对象(引用类型)的叫做容器. 为什么出现集合类? 面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的 ...

  5. iOS App内存优化之 解决UIImagePickerController的图片对象占用RAM过高问题

    这个坑会在特定的情况下特别明显: 类似朋友圈的添加多张本地选择\拍照 的图片 并在界面上做一个预览功能 由于没有特别的相机\相册需求,则直接使用系统自带的UIImagePickerController ...

  6. React 实践项目 (五)

    React在Github上已经有接近70000的 star 数了,是目前最热门的前端框架.而我学习React也有一段时间了,现在就开始用 React+Redux 进行实战! React 实践项目 (一 ...

  7. Java 程序员技能导图 1.0

    做Java开发已经一年,并非科班出身,在毕业工作三年后毅然决然辞职,参加培训机构从零开始.在这期间迷茫.失望.绝望时常伴我左右,但是在不断自我提高与努力中渐渐看到一些小小的成果使我不断坚信自己的选择并 ...

  8. 权限管理学习 一、ASP.NET Forms身份认证

    说明:本文示例使用的VS2017和MVC5. 系统无论大小.牛逼或屌丝,一般都离不开注册.登录.那么接下来我们就来分析下用户身份认证. 简单实现登录.注销 以前在学习.net的时候不知道什么Forms ...

  9. 如何免费使用jrebel 和eclipse 项目配合完成热部署功能

    天,感谢王同学分享了热部署插件,jrebel,说修改后台代码可以不用重启tomcat,于是立即下载使用....本来很简单的一个事情,因为参照了网上各种帖子,结果坑的不行....所以把自己的经验分享一下 ...

  10. 利用百度API(js),通过地址获取经纬度的注意事项

    网上给的很多答案都是这种: http://api.map.baidu.com/geocoder?address=地址&output=输出格式类型&key=用户密钥&city=城 ...