【发布时间】:2011-06-30 02:35:34
【问题描述】:
如何让 php 只返回文件的一些字节?就像,我想将字节 7 到 15 加载到字符串中,而不读取文件的任何其他部分?重要的是我不需要将所有文件加载到内存中,因为文件可能非常大。
【问题讨论】:
如何让 php 只返回文件的一些字节?就像,我想将字节 7 到 15 加载到字符串中,而不读取文件的任何其他部分?重要的是我不需要将所有文件加载到内存中,因为文件可能非常大。
【问题讨论】:
可以通过 offset 和 maxlen 参数使用file_get_contents()。
$data = file_get_contents('somefile.txt', false, NULL, 6, 8);
【讨论】:
false 而不是null。
【讨论】:
fread() 和str_split() 发布一个稍微笨拙的答案。完全忽略了fseek()。这要简洁得多。谢谢,以后一定要记住!
fopen('somefile.txt', 'rb')
使用梨:
<?php
require_once 'File.php';
//read and output first 15 bytes of file myFile
echo File::read("/path/to/myFile", 15);
?>
或者:
<?php
// get contents of a file into a string
$filename = "/path/to/myFile";
$handle = fopen($filename, "r");
$contents = fread($handle, 15);
fclose($handle);
?>
无论哪种方法,您都可以使用字节 7-15 来做您想做的事。我认为如果不从文件的开头开始,你就不能追踪某些字节。
【讨论】: