Read file in sections

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.
          }
fclose($fh);

Open in new window

Thanks in advance
tonelm54Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris HarteThaumaturgeCommented:
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.


http://uk3.php.net/manual/en/function.file-get-contents.php
0
tonelm54Author Commented:
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
		}
	else
		{
		$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
0
Ray PaseurCommented:
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:

fopen()
$num = 0
while ($num < 5000)
   while (!feof())
       $num++
        fread()
        PROCESSING, WHATEVER
fwrite() each 5000 lines
Loop
0
Chris HarteThaumaturgeCommented:
There is a lot wrong with your code, the main thing being filenames. You cannot use direct addressing ie

 "c:\\myFile.doc"

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

/var/www/html/

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);
    $x++;
    file_put_contents('output.txt', $chunk, FILE_APPEND);
//    echo $chunk;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
@Munterman: I think file_get_contents() and the variants can operate on a URL.
http://php.net/manual/en/function.file-get-contents.php

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.
http://php.net/manual/en/function.file-put-contents.php
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.