无法上传图片 - 请求实体太大请求的资源

时间:2015-07-09 22:23:40

标签: php image upload

在我的情况下,我有使用resize方法的图片上传表格,但它失败了。我有下面的代码,但不知道为什么问题出在哪里。你有什么想法?我正在使用PHP的codeingiter。以下是显示的错误。我在goDaddy上托管了这个网站。

请求实体太大请求的资源 /index.php/newPost/createNewPost/2/Raymond Chiu /不允许使用GET请求的请求数据,或者请求中提供的数据量超过容量限制。此外,尝试使用ErrorDocument处理请求时遇到500内部服务器错误错误。

//----font end php html code with javascript------------//

 <input id="image1" name="image1" class="file" type="file" accept="image/*">
 <button type="submit" class="btn btn-primary btn-tw" onclick="setup(); return false;">Submit</button>

<script>

$("#image1").fileinput({
        'showPreview' : true,
        'allowedFileExtensions' : ['jpg', 'png','gif'],
        'showUpload' : false,
        'maxFileCount':1,
        'maxFileSize': 800000,
    });
    $('#image1').on('fileclear', function(event) {
        img1 = null;
    });
    $('#image1').on('fileloaded', function(event, file, previewId, index, reader) {
          img1 = file;
          $("#uploadImgError").html('');
    });

function getResizeImage(pic, callback)
    {
        var reader = new FileReader();
        // Set the image once loaded into file reader
        reader.onload = function(e)
        {
            // Create an image
            //-----------------------------Image-------------------------
            var img = document.createElement("img");
            img.src = e.target.result;

            var canvas = document.createElement("canvas");
            //var canvas = $("<canvas>", {"id":"testing"})[0];
            var ctx = canvas.getContext("2d");
            ctx.drawImage(img, 0, 0);

            var MAX_WIDTH = 800;
            var MAX_HEIGHT = 800;
            var width = img.width;
            var height = img.height;

            if (width > height) {
                if (width > MAX_WIDTH) {
                    height *= MAX_WIDTH / width;
                    width = MAX_WIDTH;
                }
            } else {
                if (height > MAX_HEIGHT) {
                    width *= MAX_HEIGHT / height;
                    height = MAX_HEIGHT;
                }
            }
            canvas.width = width;
            canvas.height = height;
            var ctx = canvas.getContext("2d");
            ctx.drawImage(img, 0, 0, width, height);

            var dataurl = canvas.toDataURL("image/png");
            //document.getElementById('image').src = dataurl; 
            callback(dataurl);
        }
        reader.readAsDataURL(pic);
    }

 function isEmptyUploadFile(callback)
    {
        if(img1 == null && img2 == null && img3 == null && img4 == null && img5 == null)
            callback(true);
        else
            callback(false);

    }
function setForm(callback)
    {
        setImg1(function(data1)
        {
            if(data1 == true)
            {
                $('.progress-bar').css('width', 100+'%').attr('aria-valuenow', 100);
                callback(true);
            }
        });
    }

function setImg1(callback)
    {
        if(img1 != null)
        {
           getResizeImage(img1,function(tempPic)
            {
                document.getElementById('img1').value = tempPic;
                getResizeThumbnailImage(img1,function(tempTPic)
                {
                    document.getElementById('timg1').value = tempTPic;
                    $('.progress-bar').css('width', 20+'%').attr('aria-valuenow', 20); 
                    callback(true);
                });
            }); 
        }else{
            $('.progress-bar').css('width', 20+'%').attr('aria-valuenow', 20); 
            callback(true);
        }
    }

function setup()
    {
        var myform = document.getElementById("newPost");
    //console.log(myform.checkValidity());

    if(!myform.checkValidity())
    {
            document.getElementById("validate").click();
            return false;
    }
        isEmptyUploadFile(function(r)
        {
            var up1 = document.getElementById('image1').value;
            var up2 = document.getElementById('image2').value;
            var up3 = document.getElementById('image3').value;
            var up4 = document.getElementById('image4').value;
            var up5 = document.getElementById('image5').value;

            if(r == true || (up1 == "" && up2 == "" && up3 == "" && up4 == "" && up5 == "") )
            {
               $("#uploadImgError").html('<em><span style="color:red"> <i class="icon-cancel-1 fa"></i> Please Upload at least one image!</span></em>');
               //var loc = document.getElementById('uploadImgError'); //Getting Y of target element
               //window.scrollTo(0, loc);
               location.href = "#uploadImgError";                 //Go to the target element.
               return false; 
            }
            else if(r == false)
            {
                $('#pleaseWaitDialog').modal('show');
                setForm(function(data)
                {
                    console.log(data);
                    if(data == true)
                    {
                        document.getElementById("newPost").submit();
                    }
                    return data;
                });
            }
        });
    }

</script>

// ------- controller ---------------------------//
 $image1 = $this->input->post('img1'); 
 $timage1 = $this->input->post('timg1'); 

$imgPath = '';
        $timgPath = '';
        $date=date_create();
        if (isset($image1)) {
            $currentTimeStamp = date_timestamp_get($date);
            $imgPath = $upload_dir . '/['.$currentTimeStamp.']['.$userName.'].png';
            $result = $this->uploadImg($image1, false, $imgPath);

        }
        if (isset($timage1)) {
            $currentTimeStamp = date_timestamp_get($date);
            $timgPath = $upload_dir . '/[T]['.$currentTimeStamp.']['.$userName.'].png';
            $result = $this->uploadImg($timage1, true, $timgPath);
           }

function uploadImg($input, $isThumbnail, $file)
    {

        if($input == null || $input == "")
        {
            return false;
        }
        $stringVal = $input; 
        $value  = str_replace('data:image/png;base64,', '', $stringVal);

        if ($this->check_base64_image($value) == false) {
            return false;
        }

        $actualFile  = base64_decode($value);
        $img = imagecreatefromstring($actualFile);
        $imgSize = getimagesize('data://application/octet-stream;base64,'.base64_encode($actualFile));


        if ($img == false) {
            return false;
        }else
        {

            /*** maximum filesize allowed in bytes ***/
            $max_file_length  = 100000;
            $maxFilesAllowed = 10;

            log_message('debug', 'PRE UPLOADING!!!!!!!!');

            if (isset($img)){

                log_message('debug', 'UPLOADING!!!!!!!!');

                // check the file is less than the maximum file size
                if($imgSize['0'] > $max_file_length || $imgSize['1'] > $max_file_length)
                {
                    log_message('debug', 'size!!!!!!!!'.print_r($imgSize));
                    $messages = "File size exceeds $max_file_size limit";
                    return false;
                }else if (file_exists($file)) {
                    return false;
                }else
                {
                    return true;
               }
    }

function check_base64_image($base64) {
        $img = imagecreatefromstring(base64_decode($base64));
    // this code said null value.

        if (!$img) {
            return false;
        }

        imagepng($img, 'tmp.png');
        $info = getimagesize('tmp.png');

        unlink('tmp.png');

        if ($info[0] > 0 && $info[1] > 0 && $info['mime']) {
            return true;
        }

        return false;
    }

    //-------------------end --------//

2 个答案:

答案 0 :(得分:0)

您必须将服务器配置为接受大文件。

此外,您正在使用GET.Use Post。
请参阅So why should we use POST instead of GET

答案 1 :(得分:0)

我可能会找出原因。在我从codeigniter会话中使用PHP的本机会话后,我发现当我上传小图像时会损坏而无法看到图片。使用上面的代码在客户端调整大小后上载的图像文件。 应该是理由??