[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

error with allowed memory

Posted on 2009-04-27
8
Medium Priority
?
250 Views
Last Modified: 2012-05-06
hello there,
how can I prevent an error with allowed size memory..
I have a forum that im checking for valid urls from a mysql query for todays posts..
with this function a url is requested and based on the answer I mark it as good or bad url.

>>Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 25054974 bytes) in script.php on line 23

it usually happens when a link it a video with big size or an exe file or something..
<?php
 
function curl($link)
{
	global $page;	
 
	$ch = curl_init($link);
	curl_setopt($ch, CURLOPT_URL, $link);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6 (.NET CLR 3.5.30729)");
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
	curl_setopt($ch, CURLOPT_TIMEOUT, 20);
	curl_setopt($ch, CURLOPT_FAILONERROR, 1);
	if(eregi("mysite2\.com" , $link))	{
		$strCookie = realpath(dirname($_SERVER['SCRIPT_FILENAME'])) . "/cookies.txt";
	  curl_setopt($ch, CURLOPT_COOKIE, 1);
	  curl_setopt($ch, CURLOPT_COOKIEJAR, $strCookie);
	  curl_setopt($ch, CURLOPT_COOKIEFILE, $strCookie);
	}
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	
	$page=curl_exec($ch);
 
	return($page);
	curl_close($ch);
	unset($link);
	unset($ch);
}
 
?>

Open in new window

0
Comment
Question by:XK8ER
  • 4
  • 3
8 Comments
 
LVL 6

Expert Comment

by:basic612
ID: 24247059
you need to increase the memory limit for PHP:

http://drupal.org/node/207036
0
 
LVL 6

Expert Comment

by:basic612
ID: 24247064
sorry, should have clarified. That drupal doc mentions 16Mb.

However your memory is capped at 64Mb at the moment. You could try putting it up to 128Mb for this particular script. Just substitute 128Mb for 16Mb in the drupal docs!
0
 
LVL 1

Author Comment

by:XK8ER
ID: 24247542
im trying to fix the issue not increase the memory!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:shobinsun
ID: 24247544
Hi,

The error:

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 25054974 bytes) in script.php on line 23

means that you have given 64mb memmory limit in php.ini file. But you have to allocate 25054974 bytes  for this purpose.  But actually that amount of memmory not allowed in this time.  So please increase the memmory_limit variable in the php.ini file to 128mb as well.

Hope this will help you.

Regards
0
 
LVL 1

Author Comment

by:XK8ER
ID: 24247626
once again.. im here because Im looking for a way to fix this issue not increase the php memory!
0
 
LVL 6

Expert Comment

by:basic612
ID: 24250034
ok - what if you change

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

to

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);

otherwise, the following function from the comments on the PHP CURL manual page is probably going to be of interest to you... you're looking for ($status['http_code'] == 200) to tell you that the link is OK:

    function getHttpResponseCode($url)
    {
        $ch = @curl_init($url);
        @curl_setopt($ch, CURLOPT_HEADER, TRUE);
        @curl_setopt($ch, CURLOPT_NOBODY, TRUE);
        @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
        @curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $status = array();
        $status = @curl_getinfo($ch);
        return $status['http_code'];
    } 

Open in new window

0
 
LVL 1

Author Comment

by:XK8ER
ID: 24250121
it wont work because im looking for a specific data in the html.. but if its an mp3 or avi file then there is the issue!
0
 
LVL 6

Accepted Solution

by:
basic612 earned 2000 total points
ID: 24250439
OK then I'd suggest ditching curl in favour of fread:

http://www.php.net/fread

you can quickly cobble a routine to retrieve manageable blocks of data from the remote files and check them for your file types. Once the file type is determined you can close the handle.

That should mean no more out of memory errors

<?php
function search_string_in_file($string='avi/mpeg',$link) {
    $handle = fopen($link, "rt");
    while (!feof($handle)) {
        if strstr($string,fread($handle, 8192)) {
            // we found the string
            fclose($handle);
            return true;
        }
    }
    fclose($handle);
    // string not found
    return false;
}
?>

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to count occurrences of each item in an array.
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.
Suggested Courses
Course of the Month18 days, 17 hours left to enroll

834 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