Để lấy danh sách tất cả các tệp từ thư mục chung, bạn có thể sử dụng Mặt tiền tệp. Để làm việc với nó, bạn cần bao gồm lớp sau
use Illuminate\Support\Facades\File;Các tệp sau có trong thư mục công cộng -
ví dụ 1
Để lấy tất cả các tệp từ thư mục chung
đầu ra
Đầu ra của đoạn mã trên là -
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 ) )ví dụ 2
Để có được tất cả các hình ảnh từ thư mục chung
đầu ra
Đầu ra cho ở trên là như sau
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 ) )ví dụ 3
Để lấy tất cả các tệp CSS từ thư mục chung -
đầu ra
Đầu ra của đoạn mã trên là -
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 ) )Ví dụ 4
Để lấy tất cả các tệp từ thư mục chung bằng phương thức scandir() -
đầu ra
Đầu ra của đoạn mã trên là -
________số 8_______Để lấy tất cả các tệp trong một thư mục, bạn có thể sử dụng phương thức "tệp ()" có sẵn từ mặt tiền "Bộ nhớ". Chạy mã bên dưới sẽ trả về một mảng các tệp trong thư mục bạn đã chỉ định
If within the directory there are subdirectories then it will be listed as a directory, to get all of the files within the subdirectory you can call the "allFiles()" method.
Do note that it supports listing nested directories.
How to specify Disk instance used?
Since some Laravel applications will have multiple disks, you can specify the disk to use like below.allFiles('path/to/directory/); // if you are useing 's3' disk Storage::disk('public')->allFiles('path/to/directory/); // if you are using 'public' disk
Xin lưu ý rằng theo mặc định, nó sẽ sử dụng cái được chỉ định trong. tệp env như bên dưới
# .env FILESYSTEM_DRIVER=localVí dụ này tập trung vào cách lấy tất cả các tệp trong thư mục laravel. Tôi muốn cho bạn thấy laravel lấy tất cả các tệp trong thư mục. Tôi đã giải thích đơn giản về laravel lấy tất cả các tên tệp trong thư mục. Bài viết này đi chi tiết về danh sách laravel tất cả các tệp trong thư mục
Bạn có thể sử dụng ví dụ này với các phiên bản laravel 6, laravel 7, laravel 8 và laravel 9
Có một số cách để lấy danh sách tất cả các tệp trong một thư mục bằng cách sử dụng mã php laravel. tôi sẽ cung cấp cho bạn một số ví dụ về mã để lấy tất cả các tệp từ thư mục. chúng tôi sẽ sử dụng mặt tiền lưu trữ laravel để lấy tệp
Vì vậy, hãy xem mã ví dụ dưới đây với đầu ra
ví dụ 1. Laravel Nhận tất cả các tệp của thư mục cụ thể
Ở đây, chúng ta sẽ sử dụng hàm files() của Storage facade. nó sẽ trả về tất cả các tệp của thư mục cụ thể
ứng dụng/Http/Bộ điều khiển/DemoController. php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class DemoController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$directory = "public";
$files = Storage::files($directory);
dd($files);
}
}
đầu ra
Array
(
[0] => public/output.pdf
[1] => public/.gitignore
[2] => public/test.pdf
)
ví dụ 2. Laravel Nhận tất cả các tệp của thư mục theo cách đệ quy
Ở đây, chúng ta sẽ sử dụng hàm allFiles() của Storage facade. nó cũng sẽ trả về tất cả các tệp của thư mục cụ thể với các tệp thư mục con