Jump to content

Recommended Posts

Hi, I just finished my masterpiece script and now its saying this  :'(:

 

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 935 bytes) in /home/caketph/public_html/php/upload.php on line 225

 

Creating my own php.ini files doesnt have any effect.

 

Is my host likely to increase it for me or will they think I am a resource hog?

 

Other alternative is to make it so my script is very memory efficient, if so what commands and methods can I use to free up memory?

 

btw, line 225 is:

${thumb.$x} = explode("<div id=\"thumb\">", $galcontents) ;

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/176561-php-memory-error/
Share on other sites

But I have to use them for the loop, here it is:

for($i = 1; $i < $galcount + 1 ; $i++) {
$handle = fopen("../" . $gallerys[$i], "r") ;
$galcontents = fread($handle, filesize("../" . $gallerys[$i])) ;
$thumbcount = preg_match_all("<div id=\"thumb\">", $galcontents, $cheese) ;
$header = explode("<!--XR1112-->", $galcontents) ;
$header = $header[0] ;
for($x = 0; $i < $thumbcount ; $x++) {

	${thumb.$x} = explode("<div id=\"thumb\">", $galcontents) ;
	${thumb.$x} = explode("<div id=\"thumb\">", ${thumb.$x}[1]) ;
	${thumb.$x} = "<div id=\"thumb\">" . ${thumb.$x}[0] ;

	array_push($thumbbuild, ${thumb.$x}) ;
}

Link to comment
https://forums.phpfreaks.com/topic/176561-php-memory-error/#findComment-930735
Share on other sites

Still get memory overload, just on a new line :( :

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 35 bytes) in /home/caketph/public_html/php/upload.php on line 238

 

not using variable variables:

for($i = 1; $i < $galcount + 1 ; $i++) {
$handle = fopen("../" . $gallerys[$i], "r") ;
$galcontents = fread($handle, filesize("../" . $gallerys[$i])) ;
$thumbcount = preg_match_all("<div id=\"thumb\">", $galcontents, $cheese) ;
$header = explode("<!--XR1112-->", $galcontents) ;
$header = $header[0] ;
for($x = 0; $i < $thumbcount ; $x++) {

	if($x == 0) {
		$thumb = explode("<div id=\"thumb\">", $galcontents) ;
		$thumb = explode("<div id=\"thumb\">", $thumb[1]) ;
		$next = $thumb[1] ;
		$thumb = "<div id=\"thumb\">" . $thumb[0] ;
	}
	else{

	}
	array_push($thumbbuild, $thumb) ;
	$thumb = explode("<div id=\"thumb\">", $next) ;
	$thumb = "<div id=\"thumb\">" . $next[0] ;
}

Link to comment
https://forums.phpfreaks.com/topic/176561-php-memory-error/#findComment-930780
Share on other sites

67M bytes is a significant amount of memory for one script to be using. For the code you posted, you would need thousands of gallery files with thousands of <div...> tags in each one.

 

The questions become, what else is your script doing up to that point and how much memory is in use before the for() loop that starts the posted code, and why aren't you using a database to hold this information instead of parsing through it each time?

 

In order to fix a memory problem, you need to start by finding out which portion of your code is using the memory.

Link to comment
https://forums.phpfreaks.com/topic/176561-php-memory-error/#findComment-930788
Share on other sites

I just read the start of one of your previous threads. One of the main points of php is to dynamically output content where you want it, when you want it, instead of hardcoding HTML that you must change every time you want to update the content on a site. To use php for what you are doing, you would store just the data (a list of images) someplace (flat file, mysql...) or even just use the list of images found in a folder(s) and then dynamically produce the output that makes up any page. Using pagination, you would have a single .php file that is used to display any selected page in the gallery. The php code would determine which set of the available images to put into <img tags on the requested page.

Link to comment
https://forums.phpfreaks.com/topic/176561-php-memory-error/#findComment-930796
Share on other sites

Hi

 

While echoing the above comment, looking at that code it appears to be doing something fairly strange:-

 

for($x = 0; $i < $thumbcount ; $x++) {

....... //some lines for the first iteration round

array_push($thumbbuild, $thumb) ;

$thumb = explode("<div id=\"thumb\">", $next) ;

$thumb = "<div id=\"thumb\">" . $next[0] ;

}

 

The red line pushes thumb onto the end of an array.

 

The green line explodes $next (set up in the first iteration) and assigns it as an array to thumb.

 

The blue line then overwrites that $thumb array with a div and the first element of $next (and $next isn't an array).

 

Seems that the green line doesn't do anything useful while the blue line just ensures that every time round the loop the same thing is assigned in the red line.

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/176561-php-memory-error/#findComment-930798
Share on other sites

Hi, thanks for your help so far.

 

I fixed the memory issue, quite simple and embarrassing!

 

for($x = 0; $i < $thumbcount ; $x++) {

if($x == 0) {

 

should be $x !

 

Anyway I'm able to test it now, i am getting the same thumbnail repeated, which coincides with what you said about the line overwriting the variable with the same thing.

 

The reason I'm not using a database to simply feed the images to the page and use pagination etc is because I want unique gallery files each with unique header tags and paragraphs of text on. I know this can be done with php too but it could get to messy for me.

Link to comment
https://forums.phpfreaks.com/topic/176561-php-memory-error/#findComment-930810
Share on other sites

This is what I had originally and trying now, but i just 2 thumbs per page  :-\ :

for($x = 0; $x < $thumbcount ; $x++) {
	if($x == 0) {
		${thumb.$x} = explode("<div id=\"thumb\">", $galcontents) ;
		${thumb.$x} = explode("<div id=\"thumb\">", ${thumb.$x}[1]) ;
		${thumb.$x} = "<div id=\"thumb\">" . ${thumb.$x}[0] ;
	}
	else{
	${thumb.$x} = explode("<div id=\"thumb\">", ${thumb.$x}[1]) ;
	${thumb.$x} = "<div id=\"thumb\">" . ${thumb.$x}[0] ;
	}

	array_push($thumbbuild, ${thumb.$x}) ;


}

Link to comment
https://forums.phpfreaks.com/topic/176561-php-memory-error/#findComment-930832
Share on other sites

Hi

 

Do not see how that will bring back anything other than 1 thumb.

 

Loops round $x. When $x is 0 it will explode $galcontents into $thumb0, then explode $thumb0[1] into $thumb0, then assign $thumb0[0] into $thumb0. Seems to me this just finds the 2nd thumbnail (identified by looking for an id of 'thumb', which suggests duplicate ids), and puts that into $thumb0.

 

Next turn round the loop it will explode $thumb1[1], but as that has never been set up it won't give you anything useful.

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/176561-php-memory-error/#findComment-930851
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.