2

Hello please how i can fix this error ?

Fatal error: Allowed memory size of 25165824 bytes exhausted

The problem I encounter is when I try to get a content of a big file from a website

my official code is :

<?php

//begin
$ch = curl_init();
curl_setopt($ch, CURLOPT_BUFFERSIZE, 8096);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://softnet.co.tz/cms/scripts/here.txt');

$content = curl_exec($ch);

curl_close($ch);

$out = fopen('result.txt', 'a');
if ($out) {
    fwrite($out, $content);
    fclose($out);
}

?>

i've already read some stuff but didn't get it solved. please help me.

vaibhavmande
  • 1,115
  • 9
  • 17
Azouz stone
  • 21
  • 1
  • 2
  • 1
    Please do not abuse the snippet tool – John Conde Jul 23 '16 at 15:04
  • If the answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. Note that there is no obligation to do this. – pah Jul 30 '16 at 20:04

2 Answers2

3

You're fetching a huge resource and storing it into a variable before flushing it into a file.

So, let's say that in your php.ini file you've the following memory limit configuration:

memory_limit = 24M

This means that you're limiting the amount of memory a PHP request can use to 24 Megabytes of main memory (25165824 bytes). So if the resource you're fetching and storing into variable $content exceeds this size, the request will fail with that error.

You can use cURL to write the resource content directly into a file. See this answer. The relevant cURL option to do it is:

curl_setopt($ch, CURLOPT_FILE, $out);

Note that $out is your file pointer, so you need to call fopen() before setting this option.

Community
  • 1
  • 1
pah
  • 4,700
  • 6
  • 28
  • 37
0

In my case of a similar error, I was POSTing a small amount of data to a remote server using the PHP Curl library. This "memory exhausted" error was caused by the remote server trying to write into a directory that didn't exist. When I created the directory, the error no longer occurred.

David Spector
  • 1,520
  • 15
  • 21