2017-05-27 168 views
1

我有基於api的圖片上傳功能。我上傳了一個子域的項目。 enter image description here圖片上傳返回500 laravel 5.4

我的JS(angularjs):

var data = new FormData(); 

     data.append('picture', $scope.picture); 
     data.append('title', $scope.title); 
     data.append('description', $scope.description); 
     data.append('captured_by', $scope.captured_by); 

     $http.post('api/SaveGallery',data,{ 
      withCredentials: true, 
      headers: {'Content-Type': undefined }, 
      transformRequest: angular.identity 

     }).then(function (data) { 
      console.log(data) 

我的控制器:

use Illuminate\Http\Request; 

use Session; 
use DB; 
use Illuminate\Database\Eloquent; 
use Illuminate\Support\Facades\Auth; 
use Exception; 
use Response; 

use Image; 
use File; 

    public function SaveGallery(Request $r) 
{ 
    if ($r->hasFile('picture')) { 
     $image = $r->file('picture'); 
     $imageName = $image->getClientOriginalName(); 
     $destinationPath = public_path('images/Gallery'); 


     $img = Image::make($image->getRealPath()); 
     $img->encode('jpg')->save($destinationPath . '/' . $imageName); 

     DB::table('gallerys') 
      ->insert([ 
       'title' => $r->title, 
       'picture'=> $imageName, 
       'description' => $r->description, 
       'captured_by' => $r->captured_by=='undefined'?'Famous Bag':$r->captured_by, 
       'create_by' => Auth::user()->username, 
       'create_date' => date('Y-m-d H:i:s') 
      ]); 

     return Response::json(['succ' => 'added'], 200); // Status code here 

} else { 
     // $imageName = null; 
     return Response::json(['picnotfound' => 'picnotfound'], 201); // Status code here 
    } 

} 

但是當我嘗試上傳的圖片,它會返回500服務器錯誤!我想將圖像存儲在圖像/圖庫路徑中。當我返回$ destinationPath在控制檯中測試時,它返回: enter image description here 如果我刪除這一行:$ img = Image :: make($ image-> getRealPath()); $ img-> encode('jpg') - > save($ destinationPath。'/'。$ imageName);

,沒關係,但圖像沒有存儲在文件夾中。我認爲Image包問題。 我試過base_path而不是public_path,因爲我的文件夾不在公共文件夾中。 注意:在我的本地主機中一切正常

回答

0

$ destinationPath應該是images/gallary,在您的目錄結構中沒有公用文件夾。製作一個公共目錄並將其中的images/gallary文件夾移入其中,或者將目標路徑更改爲

$ destinationPath = base_path('images/galary');

請讓我知道任何疑問!

+0

我提到我使用了base_path()但失敗了。如果我使用base_path()並刪除:$ img = Image :: make($ image-> getRealPath()); $ img-> encode('jpg') - > save($ destinationPath。'/'。$ imageName );並寫入移動($ destinationPath,$ imageName);並使用base_path(),然後將圖像保存在根圖像/圖庫文件夾中。但我需要使用軟件包調整圖像大小,命名干預。 – Fawel

0

Heyy!嘗試這個!

@switch ($friend['reqs_status'])    
 
@case 0:    
 
//show button already request sent     
 
@breakcase;    
 
@case 1:    
 
    //show accept and reject request button    
 
    @breakcase;   
 
    @case 2:     
 
    //meaning they are friends already    
 
    @default:    
 
    # code...     
 
    @breakcase;  
 
     @endswitch

0

$ IMG =圖像::使($圖像 - > getRealPath());

此功能從實際路徑生成圖像,因此您必須先將其保存到temperory文件夾,然後通過干預調整其大小。

$ img = Image :: make(「images/temp/filename.ext」);