我想将上传的图像存储到我的Laravel应用程序中的storage_path(存储/应用程序/图像/用户)而不是public_path(公共/图像/用户)。
我的文件系统配置如下:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'users'=> [
'driver' => 'local',
'root' => storage_path('app/images/users'),
'url' => env('APP_URL').'/storage'
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
],
],
我还在所需的路径中创建了必要的文件夹。
我的上传脚本如下:
public function upload_avatar($data)
{
$users_image_location=storage_path('images/users');
$users_image_thumb_location_1=storage_path('images/users/thumbs-100');
$users_image_thumb_location_2=storage_path('images/users/thumbs-50');
$users_image_thumb_location_3=storage_path('images/users/thumbs-30');
$image = $data['photo'];
$filename = User::create_random_name(32) . '.' . $image->extension();
$image_file = Image::make($image->getRealPath());
list($width, $height) = getimagesize($image);
if ($width > 700)
{
//Resize the image
$image_file->resize(600, 600, function ($constraint) {
$constraint->aspectRatio();
});
}
//save the image file
$image_file->save($users_image_location.'/' . $filename);
//create and store a 100px thumbnail of the image
$image_file->resize(100, 100, function($constraint) {
$constraint->aspectRatio();
})->save($users_image_thumb_location_1.'/'.$filename);
$image_file->resize(50, 50, function($constraint) {
$constraint->aspectRatio();
})->save($users_image_thumb_location_2.'/'.$filename);
$image_file->resize(30, 30, function($constraint) {
$constraint->aspectRatio();
})->save($users_image_thumb_location_3.'/'.$filename);
//return the filename of the image
return $filename;
}
但是,当我运行它时,出现以下异常,指示系统无法写入路径。
无法将图像数据写入路径(C:\ xampp \ htdocs \ NelpritadConcept \ storage \ images / users / NITA5VYtiWGSbWLmy2M6mtTpbW6XyupA.jpeg)
当我如下更改路径public_path()时:
$users_image_location=public_path('images/users');
$users_image_thumb_location_1=public_path('images/users/thumbs-100');
$users_image_thumb_location_2=public_path('images/users/thumbs-50');
$users_image_thumb_location_3=public_path('images/users/thumbs-30');
它按预期工作。
我正在Windows 10 OS,Apache上运行Laravel 5.7,PHP 7.2
我想知道如何将图像存储到存储文件夹而不是公共路径。
对于完成此操作的任何指南,我将不胜感激 问候