我无法将上传的图像存储在公共Laravel中

时间:2019-10-21 09:53:47

标签: laravel

我想上传一些图片并将其存储在public /中,但将其存储在storage / app / public中

我以前使用过php artisan link:存储

这是代码:

public function store(Request $request)
    {
        $new_file_data=[
            'small_explain'=>$request->input('small_explain'),
            'title'=>$request->input('title'),
            'paragraph_one'=>$request->input('paragraph_one'),
            'paragraph_two'=>$request->input('paragraph_two'),
            'paragraph_list'=>$request->input('paragraph_list'),
            'paragraph_three'=>$request->input('paragraph_three'),
            'important_body'=>$request->input('important_body'),
            'quote'=>$request->input('quote'),
            'author_quote'=>$request->input('author_quote'),

             //image storage

            'index_image' => $request->file('index_image')->store('/public/Images'),
            'header_image' => $request->file('header_image')->store('/public/Images' ),
            'text_image' =>$request->file('text_image')->store('/public/Images'),

        ];
        Article::create($new_file_data);

    }

3 个答案:

答案 0 :(得分:1)

首先,欢迎使用StackOverflow。

我认为您应该更深入地了解official documentation

  

文件系统配置文件位于config/filesystems.php。在此文件中,您可以配置所有“磁盘”。每个磁盘代表一个特定的存储驱动程序和存储位置。

这是您的问题。在filesystems.php文件中,您可以进行如下配置(除非您进行了任何更改):

return [

    /*
    |--------------------------------------------------------------------------
    | Default Filesystem Disk
    |--------------------------------------------------------------------------
    */

    'default' => env('FILESYSTEM_DRIVER', 'local'),

    // [...]

    /*
    |--------------------------------------------------------------------------
    | Filesystem Disks
    |--------------------------------------------------------------------------
    */

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

    ],

];

使用此配置,Laravel将把文件夹storage/app作为存储的根目录,并包含您的自定义路径(在本例中为storage/app/public/Images)。如果要将某些内容保存在公用文件夹中,请以这种方式更改配置。

return [

    /*
    |--------------------------------------------------------------------------
    | Default Filesystem Disk
    |--------------------------------------------------------------------------
    */

    'default' => 'public', // <-- Set the default disk

    // [...]

    /*
    |--------------------------------------------------------------------------
    | Filesystem Disks
    |--------------------------------------------------------------------------
    */

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => public_path(),    // <-- Change the default path to the public folder
            'url' => env('APP_URL'),    // <-- And the default URL
            'visibility' => 'public',
        ],

    ],

];

无论如何,这不是最佳实践。如您所写,您还使用了php artisan storage:link命令,该命令创建了从storage/app/public文件夹到public/storage路径的符号链接。这样,将使用asset('storage/my-public-image.jpg')之类的命令显示该文件夹中存储的所有文件。

此外,Laravel的native function $url = Storage::url('file.jpg');会根据您设置的配置(磁盘配置中的 url 参数)自动创建URL。

这就是为什么,除非在生产环境中没有创建符号链接的能力,否则将文件直接保存到公用文件夹是没有用的,并且在大多数情况下是不安全的。

答案 1 :(得分:0)

如果要使用存储功能,请遵循@llGala答案。但是,如果要使用移动功能,则可以像下面那样使用它来忽略另存为.temp

if ($request->image) {
            $file=$request->File('image');
            $ext=$file->getClientOriginalExtension();
            $filename=$user->username . '.' . $ext;
            $file->move('images/',$filename);
            $user->image=$filename;
        }

答案 2 :(得分:-2)

首先在您的cmd中运行

  php composer require intervention/image

有关更多信息,请单击此链接   [http://image.intervention.io/getting_started/installation] 使用这种方法

public function store(Request $request)
    {
        $this->validate($request, [
            'small_explain' => 'required',
            'title' => 'required',
            'paragraph_one' => 'required',
            'paragraph_two' => 'required',
            'paragraph_list' => 'required',
            'paragraph_three' => 'required',
            'important_body' => 'required',
            'quote' => 'required',
            'author_quote' => 'required',

            'index_image' => 'required|mimes:jpeg,png,bmp',
        ]);
        $image = $request->file('index_image');
        $slug = str_slug($request->title);

        if(isset($image)) {

            $currentDate = Carbon::now()->toDateString();

            $imagename = $slug.'-'.$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();

            if(!Storage::disk('public')->exists('upload')) {
                Storage::disk('public')->makeDirectory('upload');
            }

            $postImage = Image::make($image)->resize(1600, 1066)->save($imagename);
            Storage::disk('public')->put('upload/'.$imagename, $postImage);
        } else {
            $imagename = 'default.png';
        }
        $article = new Article;
        $article->images = $imagename;
        $article->small_explain = $request->small_explain;
        $article->paragraph_one = $request->paragraph_one;
        $article->paragraph_two = $request->paragraph_two;
        $article->paragraph_list = $request->paragraph_list;
        $article->paragraph_three = $request->paragraph_three;
        $article->important_body = $request->important_body;
        $article->quote = $request->quote;
        $article->author_quote = $request->author_quote;

        $article->save();
        return redirect(route('articles.index'));
    }

如果您的问题仍未解决,请给我发消息