Laravel 实时网站上的项目图像上传错误但在本地主机上成功运行
Laravel project image upload error on live website but successfully works on localhost
如果我尝试从我的本地主机编辑个人资料中的头像,它可以工作,但在我的网站上,即使代码完全相同,我也会收到此错误。这是来自错误
的日志
[2019-02-04 06:32:18] laravel.ERROR: Image source not readable {"userId":1,"email":"myemail@gmail.com","exception":"[object] (Intervention\Image\Exception\NotReadableException(code: 0): Image source not readable at /home/user/domains/mysite.com/blogv2/vendor/intervention/image/src/Intervention/Image/AbstractDecoder.php:345)
[stacktrace]
#0 /home/user/domains/mysite.com/blogv2/vendor/intervention/image/src/Intervention/Image/AbstractDriver.php(64): Intervention\Image\AbstractDecoder->init('storage/avatar/...')
#1 /home/user/domains/mysite.com/blogv2/vendor/intervention/image/src/Intervention/Image/ImageManager.php(50): Intervention\Image\AbstractDriver->init('storage/avatar/...')
#2 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(221): Intervention\Image\ImageManager->make('storage/avatar/...')
#3 /home/user/domains/mysite.com/blogv2/app/Http/Controllers/Api/UserController.php(105): Illuminate\Support\Facades\Facade::__callStatic('make', Array)
#4 [internal function]: App\Http\Controllers\Api\UserController->cropAvatar(Object(Illuminate\Http\Request))
#5 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)
#6 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('cropAvatar', Array)
#7 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Route.php(212): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\Api\UserController), 'cropAvatar')
#8 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Route.php(169): Illuminate\Routing\Route->runController()
#9 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Router.php(659): Illuminate\Routing\Route->run()
#10 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#11 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#12 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#13 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#14 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(43): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#15 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Auth\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure), 'api')
#16 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#17 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(57): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#18 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Routing\Middleware\ThrottleRequests->handle(Object(Illuminate\Http\Request), Object(Closure), 60, '1')
#19 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#20 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#21 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Router.php(661): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#22 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Router.php(636): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#23 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Router.php(602): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))
#24 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Router.php(591): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#25 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#26 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#27 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#28 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#29 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#30 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#31 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#32 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#33 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#34 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#35 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#36 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#37 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#38 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#39 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#40 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#41 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#42 /home/user/domains/mysite.com/public_html/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#43 {main}
"}
我可以在数据库中手动更新图像,但不能通过我的实时站点更新图像。这是我的上传管理器
public function fileUpload(ImageRequest $request)
{
$strategy = $request->get('strategy', 'images');
if (!$request->hasFile('image')) {
return $this->response->json([
'success' => false,
'error' => 'no file found.',
]);
}
$path = $strategy . '/' . date('Y') . '/' . date('m') . '/' . date('d');
$result = $this->manager->store($request->file('image'), $path);
return $this->response->json($result);
}
尝试更改
$image = Image::make($currentImage['relative_url']);
和
$image = Input::file($currentImage['relative_url']);
然后
$filename = $image->getClientOriginalName();
Image::make($image->getRealPath())->resize('200','200')->save($filename);
感谢上面的评论之一和这个 我找到了解决方案。
万一有人发现这个正在寻找答案,我的问题是我使用
创建了一个符号 link
php artisan storage:link
但是创建的 link 权限错误并且属于错误的用户组。
设置正确的权限 (775) 后,我可以通过 public 文件夹中的 运行 将其设置为正确的用户组。
sudo chown www-data:www-data storage
如果我尝试从我的本地主机编辑个人资料中的头像,它可以工作,但在我的网站上,即使代码完全相同,我也会收到此错误。这是来自错误
的日志[2019-02-04 06:32:18] laravel.ERROR: Image source not readable {"userId":1,"email":"myemail@gmail.com","exception":"[object] (Intervention\Image\Exception\NotReadableException(code: 0): Image source not readable at /home/user/domains/mysite.com/blogv2/vendor/intervention/image/src/Intervention/Image/AbstractDecoder.php:345)
[stacktrace]
#0 /home/user/domains/mysite.com/blogv2/vendor/intervention/image/src/Intervention/Image/AbstractDriver.php(64): Intervention\Image\AbstractDecoder->init('storage/avatar/...')
#1 /home/user/domains/mysite.com/blogv2/vendor/intervention/image/src/Intervention/Image/ImageManager.php(50): Intervention\Image\AbstractDriver->init('storage/avatar/...')
#2 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(221): Intervention\Image\ImageManager->make('storage/avatar/...')
#3 /home/user/domains/mysite.com/blogv2/app/Http/Controllers/Api/UserController.php(105): Illuminate\Support\Facades\Facade::__callStatic('make', Array)
#4 [internal function]: App\Http\Controllers\Api\UserController->cropAvatar(Object(Illuminate\Http\Request))
#5 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)
#6 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('cropAvatar', Array)
#7 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Route.php(212): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\Api\UserController), 'cropAvatar')
#8 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Route.php(169): Illuminate\Routing\Route->runController()
#9 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Router.php(659): Illuminate\Routing\Route->run()
#10 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#11 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#12 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#13 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#14 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(43): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#15 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Auth\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure), 'api')
#16 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#17 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(57): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#18 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Routing\Middleware\ThrottleRequests->handle(Object(Illuminate\Http\Request), Object(Closure), 60, '1')
#19 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#20 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#21 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Router.php(661): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#22 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Router.php(636): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#23 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Router.php(602): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))
#24 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Router.php(591): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#25 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#26 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#27 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#28 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#29 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#30 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#31 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#32 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#33 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#34 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#35 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#36 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#37 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#38 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#39 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#40 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#41 /home/user/domains/mysite.com/blogv2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#42 /home/user/domains/mysite.com/public_html/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#43 {main}
"}
我可以在数据库中手动更新图像,但不能通过我的实时站点更新图像。这是我的上传管理器
public function fileUpload(ImageRequest $request)
{
$strategy = $request->get('strategy', 'images');
if (!$request->hasFile('image')) {
return $this->response->json([
'success' => false,
'error' => 'no file found.',
]);
}
$path = $strategy . '/' . date('Y') . '/' . date('m') . '/' . date('d');
$result = $this->manager->store($request->file('image'), $path);
return $this->response->json($result);
}
尝试更改
$image = Image::make($currentImage['relative_url']);
和
$image = Input::file($currentImage['relative_url']);
然后
$filename = $image->getClientOriginalName();
Image::make($image->getRealPath())->resize('200','200')->save($filename);
感谢上面的评论之一和这个
php artisan storage:link
但是创建的 link 权限错误并且属于错误的用户组。 设置正确的权限 (775) 后,我可以通过 public 文件夹中的 运行 将其设置为正确的用户组。
sudo chown www-data:www-data storage