UniSharp / Laravel Filemanager将数据保存到数据库表

时间:2018-06-18 23:21:14

标签: php laravel tinymce

我在我的应用程序中使用Laravel文件管理器,以便用户在将文件上传到我的应用程序时有更好的体验。

我之前做过的事

在之前的设置中,我有一个存储方法,它接受文件管理器返回的文件路径,并将其存储在数据库表中,其中包含与新闻文章或事件相关的其他项目。

在这些情况下,用户将通过文件管理器上传图像,然后选择它,这将自动填充指定图像路径的文本字段。

因此,在这些场景中,我有一个类似下面的数据库,主要由用户输入填充。

Schema::create('articles', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->string('excerpt')->nullable();
    $table->mediumText('content')->nullable();
    $table->string('featuredImage')->nullable();
    $table->mediumText('featuredVideo')->nullable();
    $table->string('author')->nullable();
    $table->string('readingTime', 15)->nullable();
    $table->string('category')->nullable();
    $table->string('published')->default('pending');
    $table->boolean('featuredArticle')->default(0);
    $table->timestamps();
});

其中featuredImage是用户在上传图像并选择图像后创建的路径,返回相对图像路径。

我在尝试

我有一个场景,用户可以通过使用Laravel文件管理器将文件上传到特定文件夹来添加到图像/文档库,最初我在列表中显示:

            @foreach ($fileList as $file) 

            @php 

            // UNIX timestamp
            $time = Storage::lastModified($file); 

            // Human readable date
            $date = gmdate("d/m/Y - H:i", $time); 

            // Filesize in Bytes
            $size = Storage::size($file);

            // Filesize converted to KB
            $size = ceil ($size/1000); 

            // URL to given file
            $url = Storage::url($file); 


            $fileName = pathinfo($file)['filename']; 

            $category = pathinfo($file)['dirname']; 

            $type = pathinfo($file)['extension']; 

            $category = basename(dirname($file))

            @endphp

            <tr>
                <td>{{$type}}</td>
                <td>
                    <a href="{{ $url }}">{{ $fileName }}</a>
                </td>
                <td>{{ $category }}</td>
                <td>{{ $date }}</td>
                <td>{{ $size }}kb</td>
            </tr>


            @endforeach

正如您所看到的,这只是在存储中的文件夹中循环,并使用混合的PHP函数和Storage Fascade来显示信息。

我意识到了什么

我得出结论,这种方法很愚蠢,因为没有相关数据库表维护或监视上传的文件,所以我做了下表:

Schema::create('file_meta_data', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('department')->nullable();
        $table->string('category')->nullable();
        $table->string('type');
        $table->string('size');
        $table->string('filepath');
        $table->timestamps();
    });

这样,只要路径没有变化,管理员就可以通过管理区域中的表单添加关于类别和部门的额外数据,这样我就可以对文件进行分类,即使用户用愚蠢的名字上传它们。

我想要的是,当用户通过文件管理器将文件上传到存储文件夹时,会为该文件创建数据库记录。

我在文件管理器的文档中看到,您可以监听的事件,但是给出以下链接:https://github.com/UniSharp/laravel-filemanager-demo-events,似乎该程序包仅包含上传图像时的事件监听器。

我尝试了以下作为基本的解决方法。

<?php

namespace App\Http\Controllers;

use App\FileMetaData;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class TemplateController extends Controller
{
    public function index()
    {
        $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)
                ]
            );
        }

        return view('pages.templates-and-tools.index', compact('fileList'));
    }
}

实际上,当用户使用文件列表转到页面时,脚本会检查存储中的此文件是否有数据库条目,否则会创建它。

这是相当差的,但是,好像没有人访问该页面的文件夹和数据库很快就会失去同步,但该表必须通过Elequent模型和查询集成搜索文件。

为了解决这个问题,我必须创建一个基本上侦听任何文件类型的新事件监听器吗?

0 个答案:

没有答案
相关问题