Laravel - 从字符串路径获取文件实例

Laravel - Get instance of file from string path

我的 laravel 应用程序成功上传文件并将其存储在我希望的位置。但是,我不想将文件的 public url 存储在数据库中,而是即时获取它们(它们存储在我的 public 目录中)并显示上传链接列表。

如何仅从 File::files(public_path() . "/files/uploads"); 返回的字符串中检索文件对象,以便获取名称、大小、修改日期等?

在此先感谢您的帮助!

foreach (File::allFiles($directory) as $file)
{
    /* $file should be a Symfony\Component\Finder\SplFileInfo object */
    $file->getSize();
    $file->getFilename();
    $file->getType();
}

Documentation for the SplFileInfo。 (感谢@Bogdan!)

您可以执行以下操作:

foreach (File::files(public_path() . '/fonts') as $path)
{
    File::size($path);         // file size
    File::name($path);         // file name (no extension)
    File::extension($path);    // file extension
    File::mimeType($path);     // file mime type
    File::lastModified($path); // file last modified timestamp
    // and so on...
}

您可以在 Illuminate\Filesystem\Filesystem class API.

中查看所有可用的方法