使用 Laravel 获取目录中的文件数
Get the number of files in a directory using Laravel
我通过命令 php artisan storage:link
创建了一个符号 link 并将我的用户存储的所有文件存储在那里。每个用户都有一个文件夹,文件夹名称为 md5($user_id)
。然后,我想检索我的 blade
文件中的文件。问题是:我的用户可以有多个上传,所以我需要获取目录中的文件数。我搜索了这个问题,找到了答案 linking to this,其解决方案如下所示:
use Illuminate\Support\Facades\Storage;
$files = Storage::files($directory);
$files = Storage::allFiles($directory);
,但文档不清楚。 $directory
变量未知,我不知道该方法的根路径。那么,我的 $directory
变量应该包含什么,以便我可以包含位于 myProject/public/storage/user_uploads/$name_of_the_folder
中的图像。
And by the way, I get the $name_of_the_folder
dynamically and the
storage
folder in public
is the symlink I have created with the
above command.
您能否尝试在您的 config/filesystems.php 文件中添加一个 user_images
密钥:
'user_images' => [
'driver' => 'local',
'root' => public_path() . '/storage/',
],
之后,尝试通过以下操作在您的代码中通过 useR_images
进行访问:
Storage::allFiles('user_images');
从这里,您可以执行他们的任何操作tutorial
我通过命令 php artisan storage:link
创建了一个符号 link 并将我的用户存储的所有文件存储在那里。每个用户都有一个文件夹,文件夹名称为 md5($user_id)
。然后,我想检索我的 blade
文件中的文件。问题是:我的用户可以有多个上传,所以我需要获取目录中的文件数。我搜索了这个问题,找到了答案 linking to this,其解决方案如下所示:
use Illuminate\Support\Facades\Storage;
$files = Storage::files($directory);
$files = Storage::allFiles($directory);
,但文档不清楚。 $directory
变量未知,我不知道该方法的根路径。那么,我的 $directory
变量应该包含什么,以便我可以包含位于 myProject/public/storage/user_uploads/$name_of_the_folder
中的图像。
And by the way, I get the
$name_of_the_folder
dynamically and thestorage
folder inpublic
is the symlink I have created with the above command.
您能否尝试在您的 config/filesystems.php 文件中添加一个 user_images
密钥:
'user_images' => [
'driver' => 'local',
'root' => public_path() . '/storage/',
],
之后,尝试通过以下操作在您的代码中通过 useR_images
进行访问:
Storage::allFiles('user_images');
从这里,您可以执行他们的任何操作tutorial