PHP File: Reading File at Particular Byte Ranges



PHP File Tips - Part 3: We can read file at particular byte ranges. With this tips, you can use for seeking particular byte ranges. For this job, we combine fopen(), fseek(), fgetc(), and ftell().
<?
$file  = "test.txt";
$start = 2;
$finish = 20;

$f = fopen($file, "rb");

fseek($f, $start, SEEK_SET);

while(!(ftell($f) > $finish)){
  $data .= fgetc($f);
}
fclose($f);

echo $data;
// result:
// st line 1 test lin
?>


Series this article:
PHP File: 3 Way of Reading File
PHP File: Reading File at Particular Lines
PHP File: Reading File at Particular Byte Ranges
PHP File: Checking File or Directory
PHP File: Showing All File in A Directory
PHP File: Showing All Files and Folders in Particular Directory
PHP File: Retrieving File Statistic
PHP File: Retrieving More File Information
PHP File: Counting Lines in a File
PHP File: Counting Characters and Words in File
PHP File: Writing File
PHP File: Locking and Unlocking File
PHP File: Removing Lines From File
PHP File: Inserting Lines into a File


Tag: file, read file, byte file Category: PHP Basic Post : April 13th 2008 Read: 2,558 Bookmark and Share

blog comments powered by Disqus