Avatar of tonelm54
tonelm54

asked on 

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
PHP

Avatar of undefined
Last Comment
Ray Paseur
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of tonelm54
tonelm54

ASKER

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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo