我有基於api的圖片上傳功能。我上傳了一個子域的項目。 圖片上傳返回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在控制檯中測試時,它返回: 如果我刪除這一行:$ img = Image :: make($ image-> getRealPath()); $ img-> encode('jpg') - > save($ destinationPath。'/'。$ imageName);
,沒關係,但圖像沒有存儲在文件夾中。我認爲Image包問題。 我試過base_path而不是public_path,因爲我的文件夾不在公共文件夾中。 注意:在我的本地主機中一切正常
我提到我使用了base_path()但失敗了。如果我使用base_path()並刪除:$ img = Image :: make($ image-> getRealPath()); $ img-> encode('jpg') - > save($ destinationPath。'/'。$ imageName );並寫入移動($ destinationPath,$ imageName);並使用base_path(),然後將圖像保存在根圖像/圖庫文件夾中。但我需要使用軟件包調整圖像大小,命名干預。 – Fawel