Saravanan
Saravanan

Reputation: 65

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?

Upvotes: 0

Views: 188

Answers (2)

Misguided
Misguided

Reputation: 1312

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

Upvotes: 3

Joe
Joe

Reputation: 82654

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

Upvotes: 3

Related Questions