开发者

Getting: Allowed memory size of 33554432 bytes exhausted

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

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

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

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.

i started doing this at the form

$max_file_size = 8388608; 
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">

Here's my file upload:

<?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 => 'php.ini max file size exceeded', 
                2 => 'html form max file size exceeded', 
                3 => 'file upload was only partial', 
                4 => 'no file was attached');

// check the upload form was actual开发者_运维技巧ly 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 '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
    '"http://www.w3.org/TR/html4/strict.dtd">'."\n\n".
    '<html lang="en">'."\n".
    '   <head>'."\n".
    '       <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
    '       <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
    '   <title>Upload error</title>'."\n\n".
    '   </head>'."\n\n".
    '   <body>'."\n\n".
    '   <div id="Upload">'."\n\n".
    '       <h1>Upload failure</h1>'."\n\n".
    '       <p>An error has occured: '."\n\n".
    '       <span class="red">' . $error . '...</span>'."\n\n".
    '       The upload form is reloading</p>'."\n\n".
    '    </div>'."\n\n".
    '</html>';
    exit;
} // end error handler
?>

MakeThumbnail function()

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);
}


It sounds like it's the overall memory_limit, not the upload limit. Are you processing the image once uploaded with GD?

If so, this will be much more memory intensive on larger images if you're doing a lot of post processing on the uploads - in this case try upping the memory limit, if you're doing something much more straight forward then there is probably another cause for the large memory usage...


very simple Create a file called "php.ini" in the "wp-admin" folder of wordpress install.

Add the following text to the file:

memory_limit = 256M ;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜