简体   繁体   中英

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 but somewhere at the middle position. How do I do that?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM