php文件读取文件内容,PHP文件系统函数-读取文件内容几种方式

介绍几种php获取文件内容的方式

介绍读取文件的方式之前,我们先看一下打开文件资源和关闭资源

名字资源绑定到一个流 - fopen

关闭一个已打开的文件指针 - fclose

$handle1 = fopen("/home/rasmus/file.txt", "r");

fclose($hanle1);

$handle2 = fopen("/home/rasmus/file.gif", "wb");

fclose($handle2);

$handle3 = fopen("http://www.example.com/", "r");

fclose($handle3);

$handle4 = fopen("ftp://user:password@example.com/somefile.txt", "w");

fclose($handle4);

?>

string fread ( resource $handle , int $length )

fread() 从文件指针 handle 读取最多 length 个字节。 该函数在遇上以下几种情况时停止读取文件:

读取了 length 个字节

到达了文件末尾(EOF)

(对于网络流)一个包变为可用或者接口超时

如果流被读缓冲并且它不表示普通文件,则最多读取一个等于块大小(通常为8192)的字节数; 取决于先前缓冲的数据,返回数据的大小可能大于块大小。

先查看一下phpinfo.php文件的内容

> cat phpinfo.php

echo phpinfo();

获取方式一

$dir = dirname(__FILE__);

$file = $dir . '/phpinfo.php';

$handle = fopen($file, "r");

//一次性输出文件的内容

echo fread($handle, filesize($file)) . PHP_EOL;

fclose($handle);

获取方式二

$dir = dirname(__FILE__);

$file = $dir . '/phpinfo.php';

//按照一定的大小循环输出内容

while (!feof($handle)) { //判断是否到文件结束

echo fread($handle, 1024) .PHP_EOL; //每次输出1024字节

}

fclose($handle);

输出结果都是:

echo phpinfo();

文件内容:

55,66

77

8899

009

88

008

每行占用一个数组key

$dir = dirname(__FILE__);

$file = $dir . '/phpinfo.php';

//FILE_IGNORE_NEW_LINES 在数组每个元素的末尾不要添加换行符

//FILE_SKIP_EMPTY_LINES 跳过空行

$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

print_r($lines);

输出结果:

//$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

Array

(

[0] => 55,66

[1] => 77

[2] => 8899

[3] => 009

[4] => 88

[5] => 008

)

//$lines = file($file);

Array

(

[0] => 55,66

[1] => 77

[2] => 8899

[3] => 009

[4] => 88

[5] =>

[6] => 008

[7] =>

)

$dir = dirname(__FILE__);

$file = $dir . '/phpinfo.php';

$content1 = file_get_contents($file);

var_dump($content1);

$content = file_get_contents($file, null, null, 10, 20);

var_dump($content);

//输出结果$content1

string(35) "55,66

77

8899

009

88

008

"

//输出结果$content

string(20) "

8899

009

88

00"

文件内容:

55,66

77

8899

009

88

22

008

$dir = dirname(__FILE__);

$file = $dir . '/phpinfo.php';

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

if (!$handle) {

echo '文件指针必须是有效的';

}

while (false !== $char = fgetc($handle)) {

echo $char;

}

//输出结果

77

8899

009

88

22

008

给输出结果加上换行,更加清楚的显示:

$dir = dirname(__FILE__);

$file = $dir . '/phpinfo.php';

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

if (!$handle) {

echo '文件指针必须是有效的';

}

while (false !== $char = fgetc($handle)) {

echo $char . PHP_EOL;

}

//输出结果

5

5

,

6

6

7

7

8

8

……

fgets是从文件指针中读取一行,fgetss 只多了一个去除php和html标记的

$dir = dirname(__FILE__);

$file = $dir . '/phpinfo.php';

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

if (!$handle) {

echo '文件指针必须是有效的';

}

while (!feof($handle)) {

echo fgetss($handle, 1024);

}

fclose($handle);

//输出结果

55,66

77

8899

009

88

22

008

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/560685.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

欧几里得算法和扩展欧几里得算法详解

欧几里得算法: int gcd(int x,int y){if(y) return gcd(y,x%y);return x; }扩展欧几里得算法: 先说一个整体思路: 先求AxBygcd(A,B);的一个解x,y 然后我们可以求他的通解 然后求AxByC的通解 我们先看看怎么求AxBygcd(A,B);的一…

php class使用方法,PHP调试类Krumo使用教程

写程序最讨厌的是程序发生错误,但是却又不知道该从何debug起,我们通常会使用print_r 或者 var_dump 或者是 echo 慢慢的debug。如果你跟我一样使用PHP 5开发,现在可以使用Krumo这个简单好用的工具帮助我们做这件事情。虽然IDE也有内建的debug…

(扩展欧几里得)青蛙的约会

题意 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特…

(待定系数法)A/B

题目&#xff1a; 要求(A/B)%9973&#xff0c;但由于A很大&#xff0c;我们只给出n(nA%9973)(我们给定的A必能被B整除&#xff0c;且gcd(B,9973) 1)。 Input 数据的第一行是一个T&#xff0c;表示有T组数据。 每组数据有两个数n(0 < n < 9973)和B(1 < B < 10^…

matlab 连接数组,matlab数组操作知识点总结

其实如果单从建模来讲&#xff0c;以下大部分函数都用不到&#xff0c;但是这些都是基础。第一点&#xff1a;数组与矩阵概念的区分数组&#xff1a;与其它编程语言一样&#xff0c;定义是&#xff1a;相同数据类型元素的集合。矩阵&#xff1a;在数学中&#xff0c;矩阵(Matri…

(组合数求模=乘法逆元+快速幂) Problem Makes Problem

题目&#xff1a; As I am fond of making easier problems, I discovered a problem. Actually, the problem is ‘how can you make n by adding k non-negative integers?’ I think a small example will make things clear. Suppose n4 and k3. There are 15 solutions.…

php做图书网站,基于PHP的图书馆网站管理系统的设计与实现

《现代图书情报技术 》 年 第 期 工作交流 总第 期基于 的图书馆网站管理系统的设计与实现 郑婷婷 张 羽 辽宁大学图书馆 沈阳 【摘要 】 阐述了在 下 , 使用 十 设计并实现 了图书馆网站管理系统 。 分系统平台如何搭建 、 系统结构如何设计以及最终如何实现三大部分 。 【关键…

(小费马定理降幂)Sum

题目&#xff1a; 分析与解答&#xff1a; 参考思路&#xff1a; https://www.cnblogs.com/stepping/p/7144512.html https://blog.csdn.net/strangedbly/article/details/50996908 根据隔板定理&#xff0c;把N分成一份的分法数为C(1,n-1)&#xff0c; 把N分成两份的分法…

温度 数值模拟 matlab,西安交通大学——温度场数值模拟(matlab)

西安交通大学材料制备与成型实验——温度场数值模拟,matlab编程温度场模拟matlab代码&#xff1a;clear,clc,clfL18;L28;N9;M9;% 边长为8cm的正方形划分为8*8的格子 T0500;Tw100; % 初始和稳态温度 a0.05; % 导温系数tmax600;dt0.2; % 时间限10min和时间步长0.2s dxL1/(M-1);dy…

matlab 参数识别,[转载]自编最小二乘法的Matlab参数辨识程序(含实例)

function [sysd,sys,err] ID(Y,U,Ts)%%基于递推最小二乘法的参数辨识程序%仅针对二阶系统&#xff1a;)%出处&#xff1a;http://blog.sina.com.cn/xianfa110%---------------%Inputs:%---------------%Y nX1 vector of your model output%U nX1 vector of your model input…

(回文串全排列个数) xiaoxin juju needs help

题目 As we all known, xiaoxin is a brilliant coder. He knew palindromic strings when he was only a six grade student at elementry school. This summer he was working at Tencent as an intern. One day his leader came to ask xiaoxin for help. His leader gav…

让apache解析html里的php代码,让Apache解析html文件中的php语句

为什么要干这种事呢&#xff1f;原因在于:对于纯粹的网页来说(不涉及对于数据库的操作)&#xff0c;可以使用一些软件来生成html代码。推荐软件Axure但是&#xff0c;当生成html文件之后&#xff0c;你发现还要写php语句对数据库进行操作时&#xff0c;就会遇到一些问题。首先&…

(找循环节)Number Sequence

题目&#xff1a; A number sequence is defined as follows: f(1) 1, f(2) 1, f(n) (A * f(n - 1) B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case contains…

git获取管理员权限 windows,windows下git怎么管理权限

一、安装软件&#xff1a;msysGit-fullinstall-1.8.1.2打开之后设置安装路径&#xff0c;默认为C:\msysgit&#xff0c;如图&#xff1a;注意&#xff1a;如果要自定义安装路径&#xff0c;请不要安装在带有空格的路径以及含有中文的路径下点击“OK”以后开始安装&#xff0c;首…

(lucas) Saving Beans

题目&#xff1a; Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel family thinks that they have to solve a problem. They suppose that…

java applet程序设计,Java Applet程序设计基础

Java Applet程序设计基础Java Applet 是用Java 语言编写的一些小应用程序&#xff0c;这些程序是直接嵌入到页面中&#xff0c;由支持Java的浏览器(IE 或 Nescape)解释执行能够产生特殊效果的程序。它可以大大提高Web页面的交互能力和动态执行能力。包含Applet的网页被称为Java…

(矩阵快速幂)解所有类似Fibonacci 的题目

Description In the Fibonacci integer sequence, F0 0, F1 1, and Fn Fn − 1 Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … An alternative formula for the Fibonacci sequence is Gi…

java ssm框架登录代码,求一个SSM框架登录功能的源码,要求能运行成功

[XML] 纯文本查看 复制代码<?xml version"1.0" encoding"UTF-8"?>xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xmlns:aop"http://www.springframework.org/schema/aop"xmlns:context"http://www.springframewo…

php getdbused,PHP之购物车

该文章记录了购物车的实现代码&#xff0c;仅供参考book_sc_fns.phpinclude_once(output_fns.php);include_once(book_fns.php);include_once(db_fns.php);include_once("user_auth_fns.php");include_once("admin_fns.php");include_once("data_vali…