Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel中,如何获取公用文件夹中所有文件的列表?_Php_Laravel_Storage - Fatal编程技术网

Php 在Laravel中,如何获取公用文件夹中所有文件的列表?

Php 在Laravel中,如何获取公用文件夹中所有文件的列表?,php,laravel,storage,Php,Laravel,Storage,我想自动生成公用文件夹中所有图像的列表,但我似乎找不到任何对象可以帮助我这样做 Storage类似乎很适合这份工作,但它只允许我搜索存储文件夹中的文件,而存储文件夹位于公用文件夹之外。考虑使用。无需在Laravel5中使用帮助器类/方法过度复杂化裸骨PHP <?php foreach (glob("/location/for/public/images/*.png") as $filename) { echo "$filename size "

我想自动生成公用文件夹中所有图像的列表,但我似乎找不到任何对象可以帮助我这样做

Storage
类似乎很适合这份工作,但它只允许我搜索存储文件夹中的文件,而存储文件夹位于公用文件夹之外。

考虑使用。无需在Laravel5中使用帮助器类/方法过度复杂化裸骨PHP

<?php
foreach (glob("/location/for/public/images/*.png") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}
?>

您可以为存储类创建另一个磁盘。在我看来,这对你来说是最好的解决办法

在磁盘阵列的config/filesystems.php中添加所需文件夹。在这种情况下,公共文件夹

    'disks' => [

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

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

    's3' => '....'
然后,您可以使用存储类以以下方式在公用文件夹中工作:

$exists = Storage::disk('public')->exists('file.jpg');
$exists变量将告诉您file.jpg是否存在于public文件夹中,因为存储磁盘“public”指向项目的公用文件夹

您可以在自定义磁盘上使用文档中的所有存储方法。只需添加磁盘(“public”)部分

稍后编辑:

人们抱怨说,我的回答没有给出列出文件的确切方法,但我的意图是永远不会删除op复制/粘贴到其项目中的一行代码。如果我能用这个词,我想“教”他如何使用laravel存储器,而不是仅仅粘贴一些代码

无论如何,列出文件的实际方法是:

$files = Storage::disk('public')->files($directory);

// Recursive...
$files = Storage::disk('public')->allFiles($directory);
在我最初的回答中,配置部分和背景在上面。

Storage::disk('local')->文件('optional_dir_name')

或者只是某种类型的文件

array_filter(存储::磁盘('local')->files(),
//只有巴布亚新几内亚的
函数($item){返回strpos($item,.png');}
);

请注意,laravel磁盘有
文件()
所有文件()
<代码>所有文件都是递归的。

要列出公共目录中的所有图像,请尝试以下操作: 请看这里


要列出目录中的所有文件,请使用以下命令

  $dir_path = public_path() . '/dirname';
   $dir = new DirectoryIterator($dir_path);
  foreach ($dir as $fileinfo) {
    if (!$fileinfo->isDot()) {

    }
    else {

    }
}

请使用以下代码获取公用文件夹中特定文件夹的所有子目录。当一些人单击文件夹时,它会列出每个文件夹中的文件

控制器文件

 public function index() {

    try {

        $dirNames = array();  
        $this->folderPath = 'export'.DS.str_replace( '.', '_', $this->getCurrentShop->getCurrentShop()->shopify_domain ).DS.'exported_files';
        $getAllDirs = File::directories( public_path( $this->folderPath ) );

        foreach( $getAllDirs as $dir ) {

            $dirNames[] = basename($dir);

        }
        return view('backups/listfolders', compact('dirNames'));

    } catch ( Exception $ex ) {
        Log::error( $ex->getMessage() );
    }



}

public function getFiles( $directoryName ) {

    try {
        $filesArr = array();
        $this->folderPath = 'export'.DS.str_replace( '.', '_', $this->getCurrentShop->getCurrentShop()->shopify_domain ).DS.'exported_files'. DS . $directoryName;
        $folderPth = public_path( $this->folderPath );
        $files = File::allFiles( $folderPth ); 
        $replaceDocPath = str_replace( public_path(),'',$this->folderPath );

        foreach( $files as $file ) {

            $filesArr[] = array( 'fileName' => $file->getRelativePathname(), 'fileUrl' => url($replaceDocPath.DS.$file->getRelativePathname()) );

        }

        return view('backups/listfiles', compact('filesArr'));

    } catch (Exception $ex) {
        Log::error( $ex->getMessage() );
    }


}
路由(Web.php)

路由::get('get-files/{directoryName}','Displaybackups\BackupController@getFiles');

查看文件-列出文件夹

@foreach( $dirNames as $dirName)
    <div class="col-lg-3 col-md-3 col-sm-4 align-center">
        <a href="get-files/{{$dirName}}" class="btn btn-light folder-wrap" role="button">
            <span class="glyphicon glyphicon-folder-open folderIcons"></span>
            {{ $dirName }}
        </a>
    </div>
@endforeach
@foreach($dirName作为$dirName)
@endforeach
查看-列出文件

@foreach( $filesArr as $fileArr)
    <div class="col-lg-2 col-md-3 col-sm-4">
        <a href="{{ $fileArr['fileUrl'] }}" class="waves-effect waves-light btn green folder-wrap">
            <span class="glyphicon glyphicon-file folderIcons"></span>
            <span class="file-name">{{ $fileArr['fileName'] }}</span>
        </a>
    </div>
@endforeach
@foreach($fileArr作为$fileArr)
@endforeach

您可以通过以下方式获取所有文件:

use Illuminate\Support\Facades\Storage;

你可以用

样本响应

[
  [
    "type" => "file",
    "path" => ".gitignore",
    "timestamp" => 1600098847,
    "size" => 27,
    "dirname" => "",
    "basename" => ".gitignore",
    "extension" => "gitignore",
    "filename" => "",
  ],
  [
    "type" => "dir",
    "path" => "avatars",
    "timestamp" => 1600187489,
    "dirname" => "",
    "basename" => "avatars",
    "filename" => "avatars",
  ]
]

这是一个冗长的答案,有20票赞成。但是您没有回答“我的公用文件夹中所有图像的列表”的问题,laravel文档apge也没有真正解释Storage::allFiles()返回的内容。是的,一个数组,但是数组中有什么?如何从中获取文件名和路径?我的想法也是如此。他们没有回答这个问题。我不明白问题的作者是如何接受这个答案的。
storage\u path('app')
为您做斜杠谢谢!我创建了一个符号链接,这样我就可以通过asset('storage/imgagName.jpg')访问照片,然后通过storage::disk('local')->files()访问照片,将它们显示在视图的表格中。!警告:这仍然会触发每个文件并遍历每个文件,似乎是本地文件系统最有效的完美解决方案。如何将其转换为laravel Image Objects文件在这里可能很有用。这对我来说更容易理解
@foreach( $filesArr as $fileArr)
    <div class="col-lg-2 col-md-3 col-sm-4">
        <a href="{{ $fileArr['fileUrl'] }}" class="waves-effect waves-light btn green folder-wrap">
            <span class="glyphicon glyphicon-file folderIcons"></span>
            <span class="file-name">{{ $fileArr['fileName'] }}</span>
        </a>
    </div>
@endforeach
use Illuminate\Support\Facades\Storage;
$files = Storage::disk('local')->allFiles('public');
Storage::disk('public')->listContents();
[
  [
    "type" => "file",
    "path" => ".gitignore",
    "timestamp" => 1600098847,
    "size" => 27,
    "dirname" => "",
    "basename" => ".gitignore",
    "extension" => "gitignore",
    "filename" => "",
  ],
  [
    "type" => "dir",
    "path" => "avatars",
    "timestamp" => 1600187489,
    "dirname" => "",
    "basename" => "avatars",
    "filename" => "avatars",
  ]
]