获取:允许的内存大小为33554432字节

时间:2010-09-02 10:02:26

标签: php

致命错误:允许的内存大小为33554432字节(尝试分配12288字节)。

这是我尝试在大约2,94 mb上传图像时出现的错误。

当我在100kb上传图像时,它工作正常。这是为什么?

我如何制定限制,所以如果你上传超过xx字节,那么你会得到它太大的错误,所以我不会得到那个致命的错误。

我开始在

表单中这样做
$max_file_size = 8388608; 
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">

这是我上传的文件:

<?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 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 '<!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);
}

2 个答案:

答案 0 :(得分:4)

这听起来像是整体memory_limit,而不是上传限制。您是否使用GD上传后处理图像?

如果是这样的话,如果你在上传中进行大量的后期处理,那么对于较大的图像来说,这会占用大量内存 - 在这种情况下,如果你正在做更多的事情,请尝试增加memory limit直接然后可能是大内存使用的另一个原因...

答案 1 :(得分:0)

很简单 创建一个名为&#34; String All = "Swiming,Reading,Singing"; &#34;的文件在&#34; php.ini&#34; wordpress安装文件夹。

将以下文字添加到文件中:

wp-admin