aek/file-uploader

这是一个简单易用的文件上传库

dev-master 2022-07-04 11:44 UTC

This package is auto-updated.

Last update: 2024-09-04 16:24:37 UTC


README

这是文件上传器的官方仓库。

安装

使用composer安装aek-file-uploader

  composer require aek/file-uploader

您需要更新应用程序配置,以便注册该包,使其可以通过Laravel加载。只需更新您的config/app.php文件,在'providers'部分的末尾添加以下代码:

config/app.php

<?php

return [
    // ...
    'providers' => [
        \AEK\FileUploader\FileUploaderServiceProvider::class,
        // ...
    ],
    // ...
];

对于Laravel,您应该发布

    php artisan vendor:publish --provider="AEK\FileUploader\FileUploaderServiceProvider"
    // or
    php artisan vendor:publish

使用/示例

您应该定义目的地、可见性和列名。该包将按照您定义的可见性将文件上传到目的地,并将文件上传后的路径作为name_of_column变量的值添加到您的模型中。

use AEK\FileUploader\Traits\FileUploader;

class User extends Authenticatable
{
    use FileUploader;

    /* 
        Here you should define a variable which name is $fileUploader and this should be array.
        This array also should has another arrays which include special configurations for each column that you want to add value to the database.  
     */
    public $fileUploader = [
        [
            // this is a folder in which we will keep files
            // each column can have another folder for the keep them in a tidy
            "destination" => "users/profile_pictures",

            // this is visibility of the file "public" or "private"
            "visibility"=> "public",

            // Here, you should define column name of the database 
            "name_of_column" => "profile_picture"
        ],
        [
            "destination" => "users/credit_cards",
            "visibility"=> "private",
            "name_of_column" => "credit_card_picture"
        ]
    ];
}
Route::post('/', function (\Illuminate\Http\Request $request) {
        $user = new \App\Models\User();
        $user->name = $request->name;
        // additional values for the model
        $user->setKey("profile_picture")->saveFile($request->file("profile_picture"));
        $user->save();
});

可用方法

Warning: you have to use setKey method before all of the methods.

快速上传文件的方法。

  $user->setKey("profile_picture")->saveFile($request->profile_picture);

setKey("key")

我们指定要上传的列名。

  $user->setKey("user");

deleteFile()

它不接受任何属性。删除文件。

syncFile($file)

上传您想要上传的文件并删除之前的文件。

  $user->syncFile($request->file("profile_picture"));

isFileExist()

检查文件是否存在于目录中。

  $user->isFileExist();

getRealPathOfFile()

返回文件的存储路径;

  $user->getRealPathOfFile();

作者

致谢

  • Alp Emre Elmas