Heim  >  Artikel  >  Backend-Entwicklung  >  PHP读取文件并可支持远程文件的代码分享_PHP教程

PHP读取文件并可支持远程文件的代码分享_PHP教程

WBOY
WBOYOriginal
2016-07-21 15:15:24674Durchsuche

php读取文件

案例一

复制代码 代码如下:

$file = 'jb51.net.php';
//本案例不支持远程
$fso = fopen($file, 'r');
echo $data = fread($fso, filesize($file));
fclose($fso);
?>

fopen() 将 file 指定的名字资源绑定到一个流上.
filesize 返回文件大小的字节数,如果出错返回 FALSE.
注: 因为 PHP 的整数类型是有符号的,并且大多数平台使用 32 位整数,filesize() 函数在碰到大于 2GB 的文件时可能会返回非预期的结果.对于 2GB 到 4GB 之间的文件通常可以使用 sprintf("%u", filesize($file)) 来克服此问题.
fread() 从文件指针 handle 读取最多 length 个字节. 该函数在读取完 length 个字节数,或到达 EOF 的时候,或(对于网络流)当一个包可用时就会停止读取文件,视乎先碰到哪种情况.
说明:低版本用法!建议php5用file_get_contents

案例二
复制代码 代码如下:

$file = 'jb51.net.php';
//支持远程
$file = 'http://www.jb51.net';//
echo $data = implode('', file($file));
?>

file -- 把整个文件读入一个数组中
说明
读取二进制的文件

案例三
复制代码 代码如下:

$file = 'http://www.jb51.net';
echo file_get_contents($file);
?>

file_get_contents -- 将整个文件读入一个字符串
说明
string file_get_contents ( string filename [, int use_include_path [, resource context]])
和 file() 一样,只除了 file_get_contents() 将文件返回为一个字符串.
file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法.如果操作系统支持还会使用内存映射技术来增强性能.

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/326101.htmlTechArticlephp读取文件 案例一 复制代码 代码如下: ?php $file = 'jb51.net.php'; //本案例不支持远程 $fso = fopen($file, 'r'); echo $data = fread($fso, filesize($file)); fcl...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn