【问题标题】:Laravel's Storage::Files not finding my filesLaravel Storage::Files 找不到我的文件
【发布时间】:2019-04-21 22:53:09
【问题描述】:

我想计算存储目录中有多少图像,但似乎Storage::Files 没有找到任何图像,我已经在storage\app\public\property\*id_of_the_property* 中存储了 4 张图像并且我已经执行了php artisan storage:link 和链接至少乍一看,运行良好。

这是我的config\FilesSystems.php

'disks' => [

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

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

        '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 show($id)
    {

        $show = [];    
        $directory = 'property/'.$id;
        $files = Storage::Files($directory);
        $count = count($files);

        $show['count'] = $count;
        $show['directory'] = $directory;

        return $show;
    }

但是变量 $show['count'] 显示为 0 而它应该是 4 并且 Storage::Files 没有做任何事情因为 $show['directory'] 显示“property/1”并且 1 是我正在测试的 id .我错过了什么?

我尝试过使用:

$directory = Storage::url('property/'.$id);
/* Result "/storage/property/1" */
$directory = Storage::url('app/public/property/'.$id);
/* Result "/storage/app/public/property/1" */

$directory = Storage_path('property\\'.$id);
/* Result "D:\\xampp\\htdocs\\www\\APP\\storage\\property\\1" */
$directory = Storage_path('app/public/property/'.$id);
/* Result "D:\\xampp\\htdocs\\www\\APP\\storage\\app\\public\\property\\1" */

但也没有成功,计数仍为 0。

感谢您的帮助。

【问题讨论】:

    标签: php laravel controller storage


    【解决方案1】:

    你可以使用文件门面

    use Illuminate\Support\Facades\File;
    ...
    $images = File::allFiles(storage_path('app/public/property')); // this is recursive
    

    $images = File::files(storage_path('app/public/property/'.$id)); // this is NOT recursive
    

    【讨论】:

    • 已经尝试过了,没有成功。实际上它会犯这个错误:“preg_match() 期望参数 2 是字符串,给定数组”
    【解决方案2】:

    使用 public_path($directory);为此

    【讨论】:

    • 试过了,也没有用;计数仍为 0
    猜你喜欢
    • 2019-04-05
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多