Jump to content

Memory_limit error


bpops

Recommended Posts

Hi,

 

I've written a script and form for a user to upload a file to my website. It works perfect for relatively small files. But if I get larger (around 10 MB), I get this error:

 

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 12707261 bytes)

 

Now, I have access to php.ini, and I've altered these parameters:

 

memory_limit = 32M

max_input_time = 200

upload_max_filesize = 25M

post_max_size = 30M

 

So I would think I could have a user upload something up to 25MB. Why do I get this error? I'm especially confused since the amount it is trying to allocate is smaller than the allowed memory size! Is the memory not clearing itself or something?

 

Oh, and this is not live on my site yet, so it's NOT a problem with multiple users uploading large files at once.

Link to comment
Share on other sites

Ok, now I'm able to reproduce the same error in a different way. If I make a new file called test.php:

 

//test.php
<?php include('test.php'); ?>

 

I get this error.

 

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 8192 bytes) in /home/scriptcr/public_html/test.php on line 1

 

Obviously this isn't code I would want to use :P but it leaves me wondering how the memory is allocated and used. Does anyone know anything about this?

Link to comment
Share on other sites

Thanks for the response.

 

I still get the same error. I know the php.ini file is working because if I run phpinfo(), all my parameters are set correctly (including the 32M limit).

 

32M should be more than enough for a 10MB file (I would think!) There must be something I'm not understanding.

 

If anyone else has any thoughts, please let me know. I'm really getting frustrated :)

Link to comment
Share on other sites

Ok, I don't think my code is 'bad' per se, since it works fine for smaller files. But here is a portion of it anyway. Thanks for looking.

 

This is from the page that accepts the form submitted with the file.

 

<?php
$script_file = $_FILES['script_file'];
  
  // File Attachment
  $fileatt = $script_file['tmp_name'];
  $fileatt_type = $script_file['type'];
  $fileatt_name = $script_file['name'];
  $file_size = filesize($script_file['tmp_name']);
  
  // Check for error
  if(!$file_size){
    $error_text = 'There was a problem uploading your file.';
  } /*this catches an error if the file doesn't upload right,
        but will not catch the memory_limit error */
  // Keep filesize under 10MB
  if($file_size > 10000000){
    $error_text = 'File size must be no larger than 10MB.';
  }
?>

Link to comment
Share on other sites

Thanks for the response.

 

I still get the same error. I know the php.ini file is working because if I run phpinfo(), all my parameters are set correctly (including the 32M limit).

 

32M should be more than enough for a 10MB file (I would think!) There must be something I'm not understanding.

 

If anyone else has any thoughts, please let me know. I'm really getting frustrated :)

 

 

PHP is trying to allocate 12707261 more bytes "This number is not including" what the file is all ready calling for. I think this equals out to around 20 -22 MB total. Now let's just say you might have a memory leak in your code, then you might get this error. Try to change the values quite higher and try again. Also you may want to check your code for memory leaks. The error is exactly the way it reads. You will have to change the memory size to a higher value to run the code as is.

 

If this don't work look into a few php memory functions to add to your code like :

 

malloc ()

free()

Link to comment
Share on other sites

weekendWorker,

 

I don't see where those functions are php functions (I do recognize them from C though). PHP.net has no listing for them.

 

And not to beat a dead horse, but while I understand what you're saying about it trying to allocate more memory, it still doesn't make sense to me. I have 32MB for the memory allocation. I'm uploading a single 8MB file. Even if it's allocating twice  (or 3x) that filesize for copying purposes (?) it still shouldn't need that much. I recognize that increasing this value might fix the problem, but it seems like a 'band-aid' solution to the problem. I'd rather understand what's going on here.

 

But I do thank you very much :) since no one else around here has been able to give me any answers either.

 

Alright, to bed with me.

Link to comment
Share on other sites

These functions are used in PHP also as well as C, but are a part of Zend engine (configuration functions). I don't feel like your beating a dead horse, I have seen this problem on some of my my servers before >> which by the way are shared environments.

 

I guess you could also try caching parts of your script if possible. This might save some memory also.

Link to comment
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.