2

我想在我的 Laravel 应用程序中将上传的图像存储到 storage_path (storage/app/images/users) 而不是 public_path (public/images/users)。

我的文件系统配置如下:

'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 操作系统、Apache 上运行 Laravel 5.7、PHP 7.2

我想知道如何将图像存储到存储文件夹而不是公共路径。

我将不胜感激任何完成这项工作的指南

4

0 回答 0