Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fatal error: Allowed memory size of 33554432 bytes

Status
Not open for further replies.

pushyr

Programmer
Jul 2, 2007
159
GB
I'm getting this error message...

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 47 bytes) in /Library/.....php on line 428

line 428 has this line of code...

Code:
$lines = file($Cdir.$file_names[$i]);


what the code does...
1. reads a csv file
2. if it detects the word 'copyright' it then removes the first 15 lines
... it does this by copying the whole file content then writing the content back to the file but skipping the first 15 lines

the files that i have this issue with are larger size files
the file in question that i'm working with right now is 7.7MB

My questions are...
1. does around 7.7MB sound like a file size that is too large to be handled by php?
2. having searched the issue... other forums say that it is a poor code problem, so do you guys see anything in my code that i can improve?
3. Do you guys think there is a more efficient way for me to achieve what i'm doing?

here is my code in it's entirety...

Code:
$X = 15; // Number of lines to remove
		
$lines ='';
$lines = file($Cdir.$file_names[$i]);
$line_00 = $lines[0];
//$line_SCheader = $lines[15];
$lineID = $lines[7];

//echo '<p>line_00: '.$line_00.'</p>';
//echo '<p>line_SCheader: '.$line_SCheader.'</p>';
//echo '<p>lineID '.$lineID.'</p>';

$dateType = ''; //reset 
if (preg_match('#Copyright#is', $lineID)) { 
	
	$dateType = 1;	// Jul  4, 2010
	
	$lines = array_slice($lines, $X + 0);
	$lines = array_merge($lines);
	
	
	$search00 = array (
					'/,$/'
					);
	
	$replace00 = array (
					''
					);
	
	$lines = preg_replace($search00, $replace00, $lines); 
	
	
	// Write to file
	$file = fopen($Cdir.$file_names[$i], 'w');
	fwrite($file, implode('', $lines));
	
	fclose($file);
	
}
 
php can handle files as large as your system memory can cope with.

however php.ini prescribes the maximum memory allocation for various activities and by attempting to load big files into memory you are causing that to blart.

so ... increase your memory allocation or rewrite your code to be more memory efficient.

by the way - why would you _want_ to remove the copyright attribution of a work?
 
ok, i'll increase the memory in the php.ini

the copyright text is just a load of header blurb that appears at the top of the file before i can get to the data.

to insert the column names and rows of data into MySQL i first need to remove all this blurb as well as remove commas from the end of rows belonging to the first few lines of data.... it's basically hygiene before i can use the data
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top