bakiuploader

适用于所有文件的Uploader类

dev-master 2020-11-09 08:17 UTC

This package is auto-updated.

Last update: 2024-09-09 16:53:20 UTC


README

此包用于上传任何类型的文件

安装时输入以下命令

composer require baki/uploader

安装后,在您使用此包的页面中,需要插入以下代码部分

use Baki\Uploader;

示例

添加输入

<input type="file" name="your_name_input"> // for single file
<input type="file" name="your_name_input[]" multiple> // for multiple files

在PHP文件中,首先实例化Uploader类

$file = new Uploader($request, 'your_name_input'); // $request must be instance of Illuminate\Http\Request

此类中有2个可选参数:分隔符和随机名称。

分隔符表示所有空格将被该参数替换。默认为'-'。

随机名称(布尔值)表示文件名是否包含随机字符。默认为0。

示例
$file = new Uploader($request, 'your_name_input', '_', 1);
For multiple files on save it return like
[
    "SlReKKT0_xSMByfiU_josh_wilburne_147469_unsplash.jpg",
    "H8rBfHmB_0UE4bag3_photo_1537411809_5959db7f925d.jpg",
    "hEX1J3dy_x7UleULw_photo_1537408621655_3034354224c4.jpg"
]
-------------------------------------------------------------------------------------
For single file on save it return like "SlReKKT0_xSMByfiU_josh_wilburne_147469_unsplash.jpg"

接下来设置允许的文件MIME类型[可选](例如,对于图片)

$file->setMimeType('image/png', 'image/jpg', 'image/jpeg');
or
$file->setMimeType('image'); // All type of image pass the validation

如果您不调用此方法,可以上传任何文件。然后没有文件类型的验证。接下来设置上传文件的最大大小(数字以兆字节为单位)

$file->setFileMaxSize(20); // the maximum file size to upload is 20MB

接下来设置文件保存路径

$path = '/var/www/sites/domain.com/project/banners; // this is example
$file->setSavePath($path);

完成时调用save方法保存图片到指定路径。此方法返回文件名

$file->save();
此方法对单个文件返回类似
    "josh-wilburne-147469-unsplash.jpg"
此方法对多个文件返回类似
[
    "josh-wilburne-147469-unsplash.jpg",
    "garin-chadwick-sLxQaYfnD20-unsplash.jpg",
    "chewy-8S0cSJ1Dy9Q-unsplash.jpg"
]