从Laravel Filemanager获取文件信息

时间:2018-06-19 12:59:16

标签: php laravel

Laravel Filemanager具有可以使用EventServiceProvider挂接的事件。我在下面列出了这些内容:

  • Unisharp \ Laravelfilemanager \ Events \ ImageIsUploading
  • Unisharp \ Laravelfilemanager \ Events \ ImageWasUploaded
  • Unisharp \ Laravelfilemanager \ Events \ ImageIsRenaming
  • Unisharp \ Laravelfilemanager \ Events \ ImageWasRenamed
  • Unisharp \ Laravelfilemanager \ Events \ ImageIsDeleting
  • Unisharp \ Laravelfilemanager \ Events \ ImageWasDeleted
  • Uniharp \ Laravelfilemanager \ Events \ FolderIsRenaming
  • Unisharp \ Laravelfilemanager \ Events \ FolderWas重命名
  • Unisharp \ Laravelfilemanager \ Events \ ImageIsResizing
  • Unisharp \ Laravelfilemanager \ Events \ ImageWasResized
  • Unisharp \ Laravelfilemanager \ Events \ ImageIsCropping
  • Unisharp \ Laravelfilemanager \ Events \ ImageWasCropped

在一个例子中,他们有这个:

public function onImageWasUploaded(ImageWasUploaded $event)
{
    $path = $event->path();
    //your code, for example resizing and cropping
}

因此在这种情况下,您可以将路径保存到数据库。

我正在尝试以下操作:

<?php
namespace App\Listeners;

use Unisharp\Laravelfilemanager\Events\ImageWasUploaded;
use App\FileMetaData;
class HasUploadedListener
{
    /**
     * Handle the event.
     *
     * @param  ImageWasUploaded  $event
     * @return void
     */
    public function handle(ImageWasUploaded $event)
    {
        $name = '';
        $department = '';
        $category = '';
        $type = '';
        $size = '';

        // Get te public path to the file and save it to the database
        $filepath = $event->path();

        FileMetaData::create(
            [
                'name' => '1',
                'department' => '2',
                'category' => 3,
                'type' => 77,
                'size' => 00,
                'filepath' => $filepath
            ]
        );
    }
}

现在,我知道使用Storage()可以得到我想要填写表格的所有数据,如下所示:

$fileList = Storage::allFiles('files/shares');

// Loop through the tag array that we just created
foreach ($fileList as $files) {
    $file = FileMetaData::updateOrCreate(
        [
            'name' => basename($files),
            'department' => basename(dirname($files)),
            'category' => null,
            'type' => Storage::mimeType($files),
            'size' => Storage::size($files),
            'filepath' => Storage::url($files)
        ]
    );
}

在这种情况下,文件上传后是否可以使用存储来获取所需的数据?

0 个答案:

没有答案