如何从公共文件夹 Laravel 中获取文件列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54172367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:15:14  来源:igfitidea点击:

How to get files list from public folder Laravel

laravellaravel-5

提问by Saif Manwill

I have images in my publicfolder (NOT STORAGE):

我的public文件夹中有图像(非存储)

enter image description here

在此处输入图片说明

And I want to get all these files in a list and for each one, I do something ... How can I do that?

我想将所有这些文件放在一个列表中,对于每个文件,我都做一些事情……我该怎么做?

回答by Mozammil

You could do this in one line:

你可以在一行中做到这一点:

use File;

$files = File::files(public_path());

// If you would like to retrieve a list of 
// all files within a given directory including all sub-directories    
$files = File::allFiles(public_path()); 

For more info, check the documentation.

有关更多信息,请查看文档

Edit: The documentation is confusing. It seems, you would need to use the FileFacade instead. I will investigate a bit more, but it seems to be working now.

编辑:文档令人困惑。看来,您需要改用FileFacade。我会进行更多调查,但现在似乎可以正常工作了。

Also, the result will be an array of SplFileInfoobjects.

此外,结果将是一个SplFileInfo对象数组。

回答by Saif Manwill

Solved! I've used this function and it's work :

解决了!我已经使用了这个功能,它的工作:

// GET PUBLIC FOLDER FILES (NAME)
if ($handle = opendir(public_path('img'))) {

    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            echo $entry."<br>"; // NAME OF THE FILE
        }
    }
    closedir($handle);
}

Thanks @MyLibary :)

谢谢@MyLibary :)

回答by javad m

If you mean the publicfolder that is notin the STORAGEfolder, but, if you want to handlethis with Laravel Storage (Laravel Filesystem), You can define this folder as a Disk for filesystem and use it as follow.

如果你指的是公共文件夹是不是存储,如果你想文件夹,但是,处理与Laravel存储(Laravel文件系统)这一点,您可以定义此文件夹的磁盘进行文件系统,并用它作为后续。

In the filesystem config config/filesystems.php, diskspart add this code:

在文件系统配置中config/filesystems.phpdisks部分添加以下代码:

'disks' => [

        ...

        'public_site' => [
            'driver' => 'local',
            'root' => public_path(''),
            'visibility' => 'public',
        ],

        ...
    ],

And then you can use this commands in your app:

然后你可以在你的应用程序中使用这个命令:

$contents = Storage::get('img/file.jpg');

or to get list of files:

或获取文件列表:

$files = Storage::files('img');

$files = Storage::allFiles('img');

More details about Laravel Storage (Laravel Filesystem) is here: https://laravel.com/docs/5.8/filesystem

有关 Laravel 存储(Laravel 文件系统)的更多详细信息,请访问:https://laravel.com/docs/5.8/filesystem

Note: If you changed Public folder you can use base_path() in the Disk definition with relative Path as follow (Otherwise don't use it):

注意:如果您更改了公共文件夹,您可以在磁盘定义中使用 base_path() 和相对路径如下(否则不要使用它):

'disks' => [

        ...

        'public_site' => [
            'driver' => 'local',
            'root' => base_path('../../public'),
            'visibility' => 'public',
        ],

        ...
    ],