Solved

spitting file into segments, buffer too small

Posted on 2015-01-31
2
150 Views
Last Modified: 2015-02-06
Im trying to read a file by segments, so Ive written the following:-
        while ($buffer = fgets($handleR, $this->chunkSize)) {

            echo strlen($buffer);

Open in new window


$this->chunkSize = 1000000, however when I echo out the size of the buffer it is 151 instead 1000000.

Is there a better way to read a file via segments?

What Im trying to do it split a file into segments and process them, as the file is several Gb's in size.

Thanks in advance for any support.
0
Comment
Question by:tonelm54
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40581557
Please see: http://php.net/manual/en/function.fgets.php where it says,
Reading ends when length - 1 bytes have been read, or a newline (which is included in the return value), or an EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line.
So basically it looks like the script is reading the first line of the dataset.  You may get better results with file_get_contents() since it provides both a starting offset and a maximum length.
0
 
LVL 34

Accepted Solution

by:
Slick812 earned 500 total points
ID: 40584702
You may do well to use the PHP file read function fread (  ), you will not need to keep track of the file position, as the fread( ) will auto advance the file position on each read see manual at -
    http://php.net/manual/en/function.fread.php

$count = 1;
while (!feof($handleR)) {
    $buffer = fread($handle, $this->chunkSize);
    echo $count++.'seg length= '.strlen($buffer);
}
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question