Hi guys,

Having a problem with users uploading photos onto my site. They can upload 6 images under 5MB. The memory setting is set to 128M in my php.ini and I doubled checked by phpinfo()

upload_max_filesize = 30M ;5MBlimit*6images=30MB
post_max_size = 30M
max_execution_time = 120
max_file_uploads = 7 
memory_limit=128M

However I get the error message occasionally:
PHP Fatal error: Out of memory (allocated 80740352) (tried to allocate 14592 bytes)

Something telling me it seems my memory_limit is not 128M but being capped at around 80M. Its an apache server. My code is like this NOTE - due to lenght I have cut out the error checks and only shown it for one image check out of the 6. But here it is but I cant see my code causing the problem as most people manage to upload there images:

if(move_uploaded_file($_FILES['uploadedfile_0']['tmp_name'], $move_file_to_directoryname_large))
{
        $n_width=100;$n_height=100; /*specify height/width of thumbnail image to be*/
                                                                                        
        if($_FILES["uploadedfile_0"]["type"]=="image/gif")
        {
                    $im=imagecreatefromgif($move_file_to_directoryname_large);
                    if(!$im)
                    {
                            /*error could occur here if image was say named .gif but was another type like .jpg casing file type error*/
                            $image_resize_error++; 
                            $array_error_msg_resize[$image_resize_error]="<p class='form_error_messages'>&#8226; An unfixable error occurred with your image ('<span class='standard_span'> " .$_FILES['uploadedfile_0']['name']." </span>').</p>";
                    }
                    else
                    {
                            $width=imagesx($im); $height=imagesy($im);
                            $newsmallerimage=imagecreatetruecolor($n_width,$n_height);                 
                            imagecopyresized($newsmallerimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
                            $move_file_to_directoryname_small=$target_path_small.$newfilename;
                            if(function_exists("imagegif"))
                            {
                                    imagegif($newsmallerimage,$move_file_to_directoryname_small); 
                                    $images_db_small[0]=substr_replace($move_file_to_directoryname_small, "", 0, 24);
                            }
                            /*frees image from memory */
                            imagedestroy($newsmallerimage);
                    }
            }
            if($_FILES["uploadedfile_0"]["type"]=="image/jpeg")
            {
                            $im=imagecreatefromjpeg($move_file_to_directoryname_large); /*create from image stored in directory specified when moving from /tmp*/
                            if(!$im)
                            {
                                    /*error could occur here if image was say named .gif but was another type like .jpg casing file type error*/
                                    $image_resize_error++; 
                                    $array_error_msg_resize[$image_resize_error]="<p class='form_error_messages'>&#8226; An unfixable error occurred with your image ('<span class='standard_span'> " .$_FILES['uploadedfile_0']['name']." </span>').</p>";
                            }
                            else
                            {
                                    $width=imagesx($im);/*Original picture width is stored*/$height=imagesy($im);/*Original picture height is stored*/
                                    $newsmallerimage=imagecreatetruecolor($n_width,$n_height);                 
                                    $imagecopyresized($newsmallerimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
                                    $move_file_to_directoryname_small=$target_path_small.$newfilename;
                                    if(function_exists("imagejpeg"))
                                    {
                                            imagejpeg($newsmallerimage,$move_file_to_directoryname_small); 
                                            $images_db_small[0]=substr_replace($move_file_to_directoryname_small, "", 0, 24);
                                    }
                                    /*frees image from memory */
                                    imagedestroy($newsmallerimage);
                            }
                }
}

Any ideas? Would this be not a code problem but server problem not having enough RAM installed perhaps?

Thanks for assistance guys and girls

What line causes the error? 80MB of RAM should be fairly enough for a simple PHP script.

What line causes the error? 80MB of RAM should be fairly enough for a simple PHP script.

The line
$im=imagecreatefromjpeg($move_file_to_directoryname_large); line 31 above

is it your server, localhost,
or does the provider host/set the ram size regardless of your .ini settings
uploaded files = 5MB , undergoing manipulation = upto 5MB, output files if all are in memory at the same time could cause problems if the host set a lower memry limit
one of my hosts sets 1 1MB limit (but its SOOOOO cheap and very reliable), so use that one for all code and host images video etc on a different host where speed does not matter, the page is loaded and everything else can just dribble in

is it your server, or does the provider host set the ram size regardless of your .ini settings

Well its not my server but the host said I can create my own custom php.ini file in public_html and it works so far, apart from this memory issue

It may be a question to ask them,
if there is a memory limit, I'm pretty sure they dont plan on 121MB+ codespace for every user on a shared host, they may ask you to sequentially load and save the images to use less ram

Check the actual configurations being used with ini_get() function. That way you can be sure what is actually being used by PHP at runtime.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.