如何在Laravel中访问公共目录?
要从公共目录获取文件列表,可以使用**File外观类**。要使用它,你需要使用以下语句包含**File**类:
use Illuminate\Support\Facades\File;
假设公共文件夹中包含以下文件:
示例1
以下程序尝试从**public_folder**检索所有图像:
<?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 ) )
示例2
要从public_folder获取所有CSS文件,你只需要将字符串'**css**'传递给**public_path()**方法。
<?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 ) )
示例3
要从公共文件夹获取所有文件,代码如下:
$path = public_path(); $files = File::allFiles($path);
**<$files>**变量是一个数组,包含public/文件夹中所有文件的详细信息。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; 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 ) )
示例4
你可以使用**scandir()**来获取public/images文件夹中的所有文件。操作方法如下:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class UserController extends Controller { public function index() { $path = public_path('images'); $files = scandir($path); print_r($files); } }
输出
以上代码的输出为:
Array ( [0] => . [1] => .. [2] => flower.jpg )
广告