【问题标题】:Laravel get file name from folder with File methodLaravel 使用 File 方法从文件夹中获取文件名
【发布时间】:2021-02-05 04:09:49
【问题描述】:

不知道如何从````filePath```中的文件夹中获取file name

use Illuminate\Support\Facades\File;

    $filePath = storage_path('app/apiFiles/'.auth()->user()->id_message.'/');

    $fileName = File::files($filePath);

    dd($fileName);

dd($fileName) 返回数组但我只需要filename 可以从数组中分离出来吗?

我需要的只是这个filename: "Z1iZ03gEhltPZj2Z2Rxnnuga2eXywheL4pQc5q0I.zip"

 array:1 [▼
    0 => Symfony\Component\Finder\SplFileInfo {#293 ▼
    -relativePath: ""
    -relativePathname: "Z1iZ03gEhltPZj2Z2Rxnnuga2eXywheL4pQc5q0I.zip"
    path: "/var/www/html/domain/storage/app/apiFiles/910960"
    filename: "Z1iZ03gEhltPZj2Z2Rxnnuga2eXywheL4pQc5q0I.zip"
    basename: "Z1iZ03gEhltPZj2Z2Rxnnuga2eXywheL4pQc5q0I.zip"
    pathname: "/var/www/html/domain/storage/app/apiFiles/910960/Z1iZ03gEhltPZj2Z2Rxnnuga2eXywheL4pQc5q0I.zip"
    extension: "zip"
    realPath: "/var/www/html/domain/storage/app/apiFiles/910960/Z1iZ03gEhltPZj2Z2Rxnnuga2eXywheL4pQc5q0I.zip"
   aTime: 2020-10-22 06:46:37
   mTime: 2020-10-22 06:46:37
   cTime: 2020-10-22 06:46:37
   inode: 1308192
   size: 3180822
   perms: 0100644
   owner: 33
   group: 33
   type: "file"
   writable: true
   readable: true
   executable: false
   file: true
   dir: false
   link: false
 }

]

【问题讨论】:

  • use Illuminate\Support\Facades\Storage; $files = Storage::files($directory); 会起作用吗?
  • @SiddharajsinhZala dd($files) 返回空 []
  • 该文件夹是否有多个文件?
  • @SiddharajsinhZala 只有一个文件Z1iZ03gEhltPZj2Z2Rxnnuga2eXywheL4pQc5q0I.zip
  • 那你可以检查数组是否为空?如果不为空,则从 [0] 索引中获取文件名。有什么问题?

标签: php file laravel-5 filenames


【解决方案1】:

解决方案,很奇怪,但对我的项目来说已经足够了

 $filePath = storage_path('app/Files/'.auth()->user()->id_message.'/');

dd($filePath)return -> /var/www/html/domain/storage/app/apiFiles/910960/

目录910960只有一个文件hash.zip

从目录中获取文件名:

$fileNameArray = preg_grep("~\.(zip)$~", scandir($filePath));

dd($fileNameArray) return -> 带有"Z1iZ03gEhltPZj2Z2Rxnnuga2eXywheL4pQc5q0I.zip"的数组

最后一步是将数组转换为字符串:

$fileNameString = implode("", $fileNameArray);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多