Solved

spitting file into segments, buffer too small

Posted on 2015-01-31
2
145 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 109

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

828 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