图片上传返回500 laravel 5.4
问题描述:
我有基于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,因为我的文件夹不在公共文件夹中。 注意:在我的本地主机中一切正常
答
$ destinationPath应该是images/gallary,在您的目录结构中没有公用文件夹。制作一个公共目录并将其中的images/gallary文件夹移入其中,或者将目标路径更改为
$ destinationPath = base_path('images/galary');
请让我知道任何疑问!
答
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
答
$ IMG =图像::使($图像 - > getRealPath());
此功能从实际路径生成图像,因此您必须先将其保存到temperory文件夹,然后通过干预调整其大小。
$ img = Image :: make(“images/temp/filename.ext”);
我提到我使用了base_path()但失败了。如果我使用base_path()并删除:$ img = Image :: make($ image-> getRealPath()); $ img-> encode('jpg') - > save($ destinationPath。'/'。$ imageName );并写入移动($ destinationPath,$ imageName);并使用base_path(),然后将图像保存在根图像/图库文件夹中。但我需要使用软件包调整图像大小,命名干预。 – Fawel