【问题标题】:save image in public folder instead storage laravel 5将图像保存在公共文件夹中,而不是存储 laravel 5
【发布时间】:2017-07-22 13:54:03
【问题描述】:

我想将我的头像保存在“Public”文件夹中然后检索。

好的。我可以保存它,但在“storage/app”文件夹中而不是“public”

我的朋友告诉我去“config/filesystem.php”并编辑它,所以我这样做了

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

还是没有变化。

这里是我的简单代码

路线:

Route::get('pic',function (){
return view('pic.pic');
});
Route::post('saved','test2Controller@save');

控制器

public function save(Request $request)
{
        $file = $request->file('image');
        //save format
        $format = $request->image->extension();
        //save full adress of image
        $patch = $request->image->store('images');

        $name = $file->getClientOriginalName();

        //save on table
        DB::table('pictbl')->insert([
            'orginal_name'=>$name,
            'format'=>$base,
            'patch'=>$patch
        ]);

        return response()
               ->view('pic.pic',compact("patch"));
}

查看:

{!! Form::open(['url'=>'saved','method'=>'post','files'=>true]) !!}
                {!! Form::file('image') !!}
                {!! Form::submit('save') !!}
            {!! Form::close() !!}

                <img src="storage/app/{{$patch}}">

如何将我的图像(和将来的文件)保存在公共文件夹而不是存储?

【问题讨论】:

    标签: php sql database image laravel


    【解决方案1】:

    您可以将磁盘选项传递给\Illuminate\Http\UploadedFile 类的方法:

    $file = request()->file('image');
    $file->store('toPath', ['disk' => 'public']);
    

    或者您可以创建新的文件系统磁盘并将其保存到该磁盘。

    您可以在config/filesystems.php创建一个新的存储盘:

    'my_files' => [
        'driver' => 'local',
        'root'   => public_path() . '/myfiles',
    ],
    

    在控制器中:

    $file = request()->file('image');
    $file->store('toPath', ['disk' => 'my_files']);
    

    【讨论】:

    • 与老旧的file_put_contents()相比,这真是太复杂了@
    【解决方案2】:

    在 config/filesystems.php 中,你可以这样做... 更改公共中的根元素

    'disks' => [
       'public' => [
           'driver' => 'local',
           'root'   => public_path() . '/uploads',
           'url' => env('APP_URL').'/public',
           'visibility' => 'public',
        ]
    ]
    

    您可以通过

    访问它
    Storage::disk('public')->put('filename', $file_content);
    

    【讨论】:

    • 我只是复制您的评论,这里我的错误是:“不支持驱动程序 []。”司机当然是“本地人”
    • tnx bor.我只是创建新项目并更改配置,最后它工作了。
    • 如何轻松获取 $file_content 变量?我不明白为什么使用框架存储文件如此复杂......
    • 我不得不将此行更改为env('APP_URL').'/public/uploads',
    • @GonzaloFS 您需要在第二个参数而不是文件对象中提供文件内容,试试这个:Storage::disk('local')-&gt;put($newFilename, file_get_contents($file));
    【解决方案3】:

    您需要link your storage directory to your public folder

    php artisan storage:link
    

    完成此操作后,要将其显示在视图中,您可以执行以下操作:

    {{ asset('storage/file.txt') }}
    

    或者在你的情况下:

    <img src="{{ asset('storage/app/' . $patch) }}">
    

    【讨论】:

    • 我做了但什么也没做。这是我的完整图片地址 => "127.0.0.1:8000/storage/app/images/…" 并且该文件存在但未显示。我只需要将文件保存到公共文件夹或以某种方式将图像移动到公共
    • 此答案适用于默认的公共文件配置。我会将您的 public 数组更改回默认值并再次保存您的文件。
    • 你的意思是“filesystems.php”?但为什么不为我工作?困惑
    • 您上传的文件是否在public/storage/images/ 文件夹中?如果没有,你没有正确保存它,如果没有更多的工作它就不会显示出来。但是,如果您按照我的回答将filesystem.php 恢复为默认值,它将开始工作。
    • 我的文件上传在这里“my-Project_folder/storage/app/”,但我想在这里上传“my-Project_folder/public/”,我的filesystem.php现在是默认的,但没有变化。这里我的文件系统内容“'public' => ['driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage' , '可见性' => '公开', ],
    猜你喜欢
    • 1970-01-01
    • 2016-07-02
    • 2018-03-24
    • 2019-12-29
    • 2020-10-03
    • 2017-07-26
    • 2021-09-08
    • 2014-02-03
    • 2020-05-20
    相关资源
    最近更新 更多