文件不存储

时间:2019-12-28 06:58:34

标签: php laravel

在上传文件时,我创建了目录,没有错误,但是图片不存储;我只看到空文件夹。

我知道我缺少一些愚蠢的东西,但是我看不到到底是什么

Controller.php

 if($request->hasFile('picture')){
     //Create a directory if doesn't exist
     $dir = 'storage/images/students/'.$campus.'/'.$program.'/'.$group.'/';
     if (!File::isDirectory(public_path($dir))){
             File::makeDirectory(public_path($dir), 0775, true, true);}

     $path       = 'images/students/'.$campus.'/'.$program.'/'.$group;
     $picture    = $request->file('picture');
     $filename   = $request->id.'.'.$picture->getClientOriginalExtension();

     $public_path = $request->file('picture')->storeAs($path, $filename,'public');
 }

filesystem.php

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

Documentation page

2 个答案:

答案 0 :(得分:0)

几乎没有改善,肯定会起作用

if($request->hasFile('picture')){

     $path = 'images/students/'.$campus.'/'.$program.'/'.$group.'/';

     //Create a directory if doesn't exist
     if (!Storage::disk('public')->exists($path)){
             File::makeDirectory(public_path($path), 0775, true, true);}


     $picture    = $request->file('picture');
     $filename   = $request->hanson_id.'.'.$picture->getClientOriginalExtension();

     $public_path = Storage::disk('public')->put( $filename, $request->file('picture'));
 }

如果您想使用自己的代码,则错过了编写存储

if($request->hasFile('picture')){
 //Create a directory if doesn't exist
 $dir = 'storage/images/students/'.$campus.'/'.$program.'/'.$group.'/';
 if (!File::isDirectory(public_path($dir))){
         File::makeDirectory(public_path($dir), 0775, true, true);}

 $path       = 'images/students/'.$campus.'/'.$program.'/'.$group;
 $picture    = $request->file('picture');
 $filename   = $request->hanson_id.'.'.$picture->getClientOriginalExtension();

 $public_path = Storage::disk('public')->put( $filename, $request->file('picture'));

}

答案 1 :(得分:0)

如下更新文件系统的配置和控制器代码

filesystem.php

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

Controller.php

if($request->hasFile('picture')){
    //Create a directory if doesn't exist
    $dir = 'storage/images/students/'.$campus.'/'.$program.'/'.$group;
    if (!File::isDirectory(public_path($dir))) {
        File::makeDirectory(public_path($dir), 0775, true, true);
    }
    $picture    = $request->file('picture');
    $filepath   = $dir . '/' . $request->hanson_id.'.'.$picture->getClientOriginalExtension();

    return Storage::disk('public')->put($filepath, $picture));
}