如何在Laravel公共文件夹中获取所有文件列表?


要获取公共文件夹中的所有文件列表,可以使用文件外观。要使用它,你需要包含以下类

use Illuminate\Support\Facades\File;

以下文件存在于公共文件夹中 −

示例1

要从公共文件夹中获取所有文件

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\File; class UserController extends Controller{ public function index() { $path = public_path(); $files = File::allFiles($path); print_r($files); } }

输出

上述代码的输出为 −

Array(
   [0] => Symfony\Component\Finder\SplFileInfo Object(
      [relativePath:Symfony\Component\Finder\SplFileInfo:private] => css
      [relativePathname:Symfony\Component\Finder\SplFileInfo:private] => css\test.css
      [pathName:SplFileInfo:private] => C:\xampp\htdocs\laraveltest\public\css\test.css
      [fileName:SplFileInfo:private] => test.css
   )
   
   [1] => Symfony\Component\Finder\SplFileInfo Object(
      [relativePath:Symfony\Component\Finder\SplFileInfo:private] =>
      [relativePathname:Symfony\Component\Finder\SplFileInfo:private] => favicon.ico
      [pathName:SplFileInfo:private] => C:\xampp\htdocs\laraveltest\public\favicon.ico
      [fileName:SplFileInfo:private] => favicon.ico
   )

   [2] => Symfony\Component\Finder\SplFileInfo Object(
      [relativePath:Symfony\Component\Finder\SplFileInfo:private] => images
      [relativePathname:Symfony\Component\Finder\SplFileInfo:private] => images\flower.jpg
      [pathName:SplFileInfo:private] => C:\xampp\htdocs\laraveltest\public\images\flower.jpg
      [fileName:SplFileInfo:private] => flower.jpg
   )
   
   [3] => Symfony\Component\Finder\SplFileInfo Object(
      [relativePath:Symfony\Component\Finder\SplFileInfo:private] =>
      [relativePathname:Symfony\Component\Finder\SplFileInfo:private] => index.php
      [pathName:SplFileInfo:private] => C:\xampp\htdocs\laraveltest\public\index.php
      [fileName:SplFileInfo:private] => index.php
   )

   [4] => Symfony\Component\Finder\SplFileInfo Object(
      [relativePath:Symfony\Component\Finder\SplFileInfo:private] =>
      [relativePathname:Symfony\Component\Finder\SplFileInfo:private] => robots.txt
      [pathName:SplFileInfo:private] => C:\xampp\htdocs\laraveltest\public\robots.txt
      [fileName:SplFileInfo:private] => robots.txt
   )
)

示例2

要从公共文件夹中获取所有图像。

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use Illuminate\Support\Facades\Storage; class UserController extends Controller{ public function index() { $images = \File::allFiles(public_path('images')); print_r($images); } }

输出

上述代码的输出如下。

Array(
   [0] => Symfony\Component\Finder\SplFileInfo Object(
      [relativePath:Symfony\Component\Finder\SplFileInfo:private] =>
      [relativePathname:Symfony\Component\Finder\SplFileInfo:private] => flower.jpg
      [pathName:SplFileInfo:private] => C:\xampp\htdocs\laraveltest\public\images\flower.jpg
      [fileName:SplFileInfo:private] => flower.jpg
   )
)

示例3

要从公共文件夹中获取所有CSS文件 −

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller{ public function index() { $css_files = \File::allFiles(public_path('css')); print_r($css_files); } }

输出

上述代码的输出为 −

Array(
   [0] => Symfony\Component\Finder\SplFileInfo Object (
      [relativePath:Symfony\Component\Finder\SplFileInfo:private] =>
      [relativePathname:Symfony\Component\Finder\SplFileInfo:private] => test.css
      [pathName:SplFileInfo:private] => C:\xampp\htdocs\laraveltest\public\css\test.css
      [fileName:SplFileInfo:private] => test.css
   )
)

示例4

要使用scandir()方法从公共文件夹中获取所有文件 −

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller{ public function index() { $path = public_path(); $files = scandir($path); print_r($files); } }

输出

上述代码的输出为 −

Array(
   [0] => .
   [1] => ..
   [2] => .htaccess
   [3] => css
   [4] => favicon.ico
   [5] => images
   [6] => index.php
   [7] => robots.txt
)

更新于: 30-8-2022

2千+浏览

职业启航

通过完成课程获得认证

开始学习
广告