• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

error with allowed memory

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
XK8ER
Asked:
XK8ER
  • 4
  • 3
1 Solution
 
basic612Commented:
you need to increase the memory limit for PHP:

http://drupal.org/node/207036
0
 
basic612Commented:
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
 
XK8ERAuthor Commented:
im trying to fix the issue not increase the memory!
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
shobinsunCommented:
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
 
XK8ERAuthor Commented:
once again.. im here because Im looking for a way to fix this issue not increase the php memory!
0
 
basic612Commented:
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
 
XK8ERAuthor Commented:
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
 
basic612Commented:
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
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.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now