Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

spitting file into segments, buffer too small

Posted on 2015-01-31
2
Medium Priority
?
173 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
2 Comments
 
LVL 111

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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

963 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