2015-07-09 26 views
0

在我的情況下,我使用調整大小方法的圖像上傳形式,但它失敗了。我有下面的代碼,但不知道爲什麼問題在哪裏。你有什麼想法?我正在使用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 --------// 
+0

您需要配置您的服務器以接受較大的文件上傳,並確保您的上傳方法是POST。 –

回答

0

您必須將您的服務器配置爲接受大文件。

此外,您正在使用GET.Use Post。
請參閱So why should we use POST instead of GET

+0

我相信我在窗體控件中使用POST。但不知道爲什麼它告訴我GET ?????? –

+0

我正在使用file_put_contents。它是GET嗎? –

0

我可能會找到原因。在codeigniter會話中使用PHP的本地會話之後,我發現當我上傳小圖像會損壞並無法看到圖片。以上代碼在客戶端調整大小後上傳的圖像文件。 應該是原因嗎?