Getting: Allowed memory size of 33554432 bytes exhausted
<p>Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 12288 bytes).</p>

<p>Thats the error i get when i try to upload a image on around 2,94 mb.</p>

<p>When i upload a image on 100kb and so it works fine. Why is this?</p>

<p>How can i make a restriction, so if you upload over xx bytes then you will get error that its too big, so i dont get that fatal error.</p>

<p>i started doing this at the form</p>

<pre><code>$max_file_size = 8388608; 
&lt;input type="hidden" name="MAX_FILE_SIZE" value="&lt;?php echo $max_file_size ?&gt;"&gt;
</code></pre>

<p>Here's my file upload:</p>

<pre><code>&lt;?php  
include "dbc.php";

$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'images/profilePhoto/';

$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'editProfile.php';

$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'home.php';

$fieldname = 'file';


// possible PHP upload errors
$errors = array(1 =&gt; 'php.ini max file size exceeded', 
                2 =&gt; 'html form max file size exceeded', 
                3 =&gt; 'file upload was only partial', 
                4 =&gt; 'no file was attached');

// check the upload form was actually submitted else print form
isset($_POST['submit'])
    or error('You need to upload a profilephoto, no?', $uploadForm);

// check for standard uploading errors
($_FILES[$fieldname]['error'] == 0)
    or error($errors[$_FILES[$fieldname]['error']], $uploadForm);

// check that the file we are working on really was an HTTP upload
@is_uploaded_file($_FILES[$fieldname]['tmp_name'])
    or error('not an HTTP upload', $uploadForm);

// validation... since this is an image upload script we 
// should run a check to make sure the upload is an image
@getimagesize($_FILES[$fieldname]['tmp_name'])
    or error('only image uploads are allowed', $uploadForm);

// make a unique filename for the uploaded file and check it is 
// not taken... if it is keep trying until we find a vacant one
$now = time();
while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
{
    $now++;
}

// now let's move the file to its final and allocate it with the new filename
makeThumbnail($_FILES[$fieldname],  122, 160, $v[id]); 
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
    or error('receiving directory insuffiecient permission', $uploadForm);
$filnamn =  $now.'-'.$_FILES[$fieldname]['name'];
    mysql_query("UPDATE users_profile SET photo = '$filnamn' WHERE uID = '$v[id]'") or die(mysql_error());
// If you got this far, everything has worked and the file has been successfully saved.
// We are now going to redirect the client to the success page.
echo "Du har nu bytt profillbild!";
// make an error handler which will be used if the upload fails
function error($error, $location, $seconds = 5)
{
    header("Refresh: $seconds; URL=\"$location\"");
    echo '&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
    '"http://www.w3.org/TR/html4/strict.dtd"&gt;'."\n\n".
    '&lt;html lang="en"&gt;'."\n".
    '   &lt;head&gt;'."\n".
    '       &lt;meta http-equiv="content-type" content="text/html; charset=iso-8859-1"&gt;'."\n\n".
    '       &lt;link rel="stylesheet" type="text/css" href="stylesheet.css"&gt;'."\n\n".
    '   &lt;title&gt;Upload error&lt;/title&gt;'."\n\n".
    '   &lt;/head&gt;'."\n\n".
    '   &lt;body&gt;'."\n\n".
    '   &lt;div id="Upload"&gt;'."\n\n".
    '       &lt;h1&gt;Upload failure&lt;/h1&gt;'."\n\n".
    '       &lt;p&gt;An error has occured: '."\n\n".
    '       &lt;span class="red"&gt;' . $error . '...&lt;/span&gt;'."\n\n".
    '       The upload form is reloading&lt;/p&gt;'."\n\n".
    '    &lt;/div&gt;'."\n\n".
    '&lt;/html&gt;';
    exit;
} // end error handler
?&gt;
</code></pre>

<p>MakeThumbnail function() </p>

<pre><code>function makeThumbnail($file, $thumbSizeWidth, $thumbSizeHeight, $user) {
    if ($file['error'] !== UPLOAD_ERR_OK) {

        // something blew up
        // so handle error condition
        // 
        // error codes documentation: http://php.net/manual/en/features.file-upload.errors.php
        die();
    }

    $path_thumbs = "images/profilePhoto/thumbs/";
    $allowed_types = array('image/jpeg', 'image/jpg', 'image/bmp', 'image/png', 'image/gif');

    $imageinfo = getimagesize($file['tmp_name']); // get image info
list($width, $height, $type, $attr) = $imageinfo;

    if ($imageinfo === FALSE) {
        die("Uhoh. Unable to read uploaded file");
    }

    if (!in_array($imageinfo['mime'], $allowed_types)) {
        die("Sorry, images of type {$imageinfo['mime']} not allowed");
    }

    $rand_name = rand(0, 999999999); // this isn't particularly well done, but ...

    // create thumbnail
    switch($imageinfo['mime']) {
        case 'image/jpeg':
        case 'image/jpg':
            $new_img = imagecreatefromjpeg($file['tmp_name']);
            $file_ext = '.jpg';
            break;
        case 'image/gif':
            $new_img = imagecreatefromgif($file['tmp_name']);
            $file_ext = '.gif';
            break;
        case 'image/png':
            $new_img = imagecreatefrompng($file['tmp_name']);
            $file_ext = '.png';
            break;
        default:
            die("Uhoh. How did we get here? Unsupported image type");
    }

    $imgratio = $height / $width;


        $newwidth = $thumbSizeWidth;
        $newheight = $thumbSizeHeight;

    $resized_img = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    $thumb_name = $rand_name . $file_ext;
    $thumb_path = $path_thumbs . '/' . $rand_name . $file_ext;
    imagejpeg($resized_img, $thumb_path);

mysql_query("UPDATE users_profile SET photo_thumb = '$thumb_name' WHERE uID = '$user'") or die(mysql_error());

    imagedestroy($resized_img);
    imagedestroy($new_img);

    return($thumb_name);
}
</code></pre>
