开发者

How to read file's content in php not from the beginning?

On Windows OS, I have a file with size of 1 MB. I want to use PHP to read the file's content. I know there is the fread function to read file, however, I want to read the file not from the starting position bu开发者_运维百科t somewhere at the middle position. How do I do that?


http://php.net/manual/en/function.fseek.php fseek


As Joey said, you have tu use fseek.

The following snippet should do (although it is untested):

<?php
$file = fopen("Myfile.txt", 'r');
if (!$file) die('error');

fseek($file, filesize($file)/2, SEEK_CUR); // Position exactly at the middle of the file

/*
 * fread() comes in here
 */
?>

EDIT: instead of filesize();, it would be better to use fstat() and then use the 'size' part of the array. http://www.php.net/manual/en/function.fstat.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜