Read file in sections

Posted on 2011-10-03
Last Modified: 2012-05-12
Im trying to open a file in my PHP script, however keep getting the message 'Out of Memory'. I only want to access the file in sections of 25Kb, so is it possible to read a file 25Kb at a time in PHP?

So something like:-

$fh = fopen('c:\myFile.txt', 'r') or die("Can't open file");
while readFile.eof=false
          $fs = fread($fh,25000)
          //Do my work on the 25 or less stream.

Open in new window

Thanks in advance
Question by:tonelm54
    LVL 16

    Expert Comment

    by:Chris Harte
    You will probably be better off using file_get_contents. This reads a file into a string more efficiently than fread and also allows offsets so you can read the file in chunks.

    Author Comment

    Good afternoon,
    Ok, so I wrote a simple code, to basically read a file in chunks:-
    	$strFile = "c:\\myFile.doc";
    	$bufferSize = 25000;
    	$rndFileID = rndID();
    	$newFileSize = filesize($strFile);
    	if ($newFileSize % $bufferSize > 0)
    		$newFileSegments = $newFileSize /$bufferSize
    		$newFileSegments = ($newFileSize /$bufferSize) + 1
    	for (x=1;x<$newFileSegments;x++)
    		$data = file_get_contents($strFile, ,null ,x*$bufferSize, $bufferSize);
    		$file_put_contents ('c:\\copiedFile.doc', $data, FILE_APPEND);

    Open in new window

    But the file is corrupt when
    LVL 107

    Expert Comment

    by:Ray Paseur
    Uhh, couple of thoughts.  File_Get_Contents() will read the entire file into memory, so this may not be what you want if you are getting "out of memory" conditions.

    The function rndid() on line 5 does not exist so we do not know what it might be doing.  It appears to create a variable that is never used.  Might want to leave that out.

    Conceptually it is much easier to count the lines in a file, versus counting the bytes.  So you might want to try something like this:

    $num = 0
    while ($num < 5000)
       while (!feof())
    fwrite() each 5000 lines
    LVL 16

    Accepted Solution

    There is a lot wrong with your code, the main thing being filenames. You cannot use direct addressing ie


    You can only use relative addressing. The files you want to use must be on the root path of your web server.

    In linux this would be


    and would be addressed with out using any redirection. This code reads a file called text.txt in 2500 bit sized chunks and outputs it to a file called output.txt.
    $strFile = "test.txt";
    $size = filesize($strFile);
    $bufferSize = 2500;
    $no_of_chunks = ceil($size / $bufferSize);
    $x = 0;
    while ($x < $no_of_chunks)
        $offset = $x * $bufferSize;
        $chunk = file_get_contents($strFile, NULL, NULL, $offset, $bufferSize);
        file_put_contents('output.txt', $chunk, FILE_APPEND);
    //    echo $chunk;

    Open in new window

    LVL 107

    Assisted Solution

    by:Ray Paseur
    @Munterman: I think file_get_contents() and the variants can operate on a URL.

    Even though the site says that file_put_contents() may operate on a URL, I have found through experience that a path based on getcwd() seems to work better.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    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.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now