PHP文件上传库

3.0 2024-07-28 04:58 UTC

This package is auto-updated.

Last update: 2024-09-28 05:19:55 UTC


README

Total Downloads Latest Stable Version License Build Status Code Coverage

文档

需求

  • >= php 8.0+

安装

在安装支持包之前,获取PHP依赖管理器Composer,因为它将简化安装过程。

composer require tamedevelopers/file

实例化

步骤1 — 使用Composer 实例化类

require_once __DIR__ . '/vendor/autoload.php';

use Tamedevelopers\File\File;

$file = new File();
  • 示例2
require_once __DIR__ . '/vendor/autoload.php';

$file = new Tamedevelopers\File\File();
  • 或者 -- 辅助函数
$file = TameFile();

Amazon Aws S3

  • 要使用s3,首先需要运行composer require aws/aws-sdk-php
  • 默认情况下,Aws将加载整个SDK,但我们不需要
    • 将以下代码复制到您的根目录composer.json中,然后在终端中运行composer update
"scripts": {
    "pre-autoload-dump": "Aws\\Script\\Composer\\Composer::removeUnusedServices"
},
"extra": {
    "aws/aws-sdk-php": [
        "Ec2",
        "CloudWatch"
    ]
}

全局配置

  • 配置全局配置,这样您就不必总是包含默认设置
    • 定义为命名参数
config_file(
    message: [
        '401'   => 'Select file to upload',
        '402'   => 'File upload is greater than allowed size of:',
        '403'   => 'Maximum file upload exceeded. Limit is:',
        '404'   => 'Uploaded file format not allowed. Allowed formats:',
        '405'   => 'Image dimension allowed is:',
        '405x'  => 'Image dimension should be greater than or equal to:',
        '200'   => 'File uploaded successfully:',
        'kb'    => 'kb',
        'mb'    => 'mb',
        'gb'    => 'gb',
        'and'   => 'and',
        'width' => 'width',
        'height'=> 'height',
        'files' => 'files',
        'file'  => 'file',
    ],
    config: [
        'limit'         => 1,
        'mime'          => 'image', // video|audio|file|image|zip|pdf|xls|doc|general_image|general_media|general_file
        'size'          => '2mb',
        'baseDir'       => 'public',
        'driver'        => 'local',
        'structure'     => 'default', // default|year|month|day
        'generate'      => true, // will always generate a unique() name for each uploaded file
    ],
    class: [
        'error'     => 'bg-danger',
        'success'   => 'bg-success',
    ]
);
  • 或者 -- 使用文件类方法全局配置
(new File)->globalConfig(
    message: [], 
    config: [],
    class: []
);

输入HTML结构

<input type="file" name="avatar">
  • 或者 -- 多数据
<input type="file" name="avatar[]" multiple>

响应数据

  • 如何检索数据

获取消息

  • 这将返回错误消息
$file = File::name('html_input_name');

$file->getMessage();

$file->getMessage('new Message');

获取状态

  • 这将返回错误状态码
$file = File::name('html_input_name');

$file->getStatus();

获取类

  • 这将只返回消息,如果存在错误或成功
$file = File::name('html_input_name');

$file->getClass();

第一个

  • 这将获取第一个上传的数据
    • [可选] 您可以将模式作为字符串name | path|url传递
->save(function($response){

    $response->first();
});
  • 或者
$upload = File::name('avatar')
                ->save();

$upload->first('url);

获取

  • 这将获取所有上传数据
    • 返回一个包含所有上传数据的索引数组,[name, path, url]
    • [可选] 您可以将模式作为字符串name | path|url传递
->save(function($response){

    $response->get();
});
  • 或者
$upload = File::name('avatar')
            ->save();

$upload->get('name);

表单

  • 返回表单请求数据
    • 这将返回validator package对象Validator
->save(function($response){

    $response->form();
});

取消链接

  • 接受两个字符串参数。此方法会自动使用基本路径。
    • [必填] $fileToUnlink要取消链接的文件路径。
    • [可选] $checkFile。这将检查在之前$fileToUnlink是否相同。
->save(function($response){

    $userAvatar;

    $response->unlink("public/images/{$userAvatar}", "avatar.svg");

    <!-- the above will only unlink when value is not avatar.svg -->
});

用法

名称

  • 接受一个输入名称的string参数
    • 默认为静态方法
File::name('html_input_name');

驱动器

  • 将来将添加更多驱动器
    • 默认情况下,驱动器设置为local
File::name('avatar')
    ->driver('s3');
  • 在没有框架的项目中使用驱动器
use Tamedevelopers\Support\Env;

// if your project is not on on core php, then you'll need to load env.
// this will create env dummy data and as well load the .env file.
// once data has been created, you can remove the `Env::createOrIgnore();`


Env::createOrIgnore();
Env::load();

File::name('avatar')
    ->driver('s3');

基本目录

  • 接受一个作为基本目录名称的string参数
    • 这将覆盖全局配置设置(域和服务器路径将设置)
File::name('avatar')
    ->baseDir('newBaseDirectory');

生成

  • 接受一个bool参数
File::name('avatar')
        ->generate(false);

文件夹

  • 接受一个作为保存文件路径的string参数
File::name('avatar')
    ->folder('upload/user');

过滤器

  • 接受索引或闭包数组索引
    • 移除你不想验证的错误状态码
    • 您不能移除错误200
File::name('avatar')
    ->filter(401, 402);
  • 或者
File::name('avatar')
    ->filter([401, 402, 405]);

结构

  • 接受一个作为结构类型string参数
    • 最佳用于Media\|Blog\|Newsletter网站。
  1. 默认值
File::name('avatar')
    ->structure('month');

大小

  • 接受一个string | int参数
    • int | kb | mb | gb为单位的大小
File::name('avatar')
    ->size('1.5mb'); // will be converted to:  1.5 * (1024 * 1024) = 1572864
  • 或者
File::name('avatar')
    ->size(2097152); // = 2097152|2mb

限制

  • 接受一个string | int参数
    • 默认限制设置为 1 次上传
File::name('avatar')
    ->limit(2);

MIME类型

  • 接受一个 string 参数作为 MIME 类型
    • 转到 MIME 类型 查看列表
File::name('avatar')
    ->mime('image');

宽度

  • 接受两个参数 string | intbool
    • 第一个参数是 string | int。宽度大小
    • 第二个参数是 bool。这允许检查大小是否应该等于或大于上传的图片大小。默认为 true
$file = File::name('avatar')
        ->width(700, false);

dd(
    $file
);

高度

  • width 方法相同
File::name('avatar')
    ->width(700)
    ->height(400);

验证

  • [可选] 可以使用该方法来返回错误信息。
    • 接受一个 [可选] 参数作为 callable\|closure 函数。
File::name('banner')
    ->folder('upload/banner')
    ->validate(function($response){

        // perform any other needed task in here
        echo $response->getMessage();
        return;
    });

保存

  • 接受一个 [可选] 参数作为 callable\|closure 函数。
    • 调用此 [方法] 将自动保存数据。
File::name('banner')
    ->folder('upload/banner')
    ->save(function($response){

        // perform any other needed task in here
    });
  • 或者
$file = File::name('banner')
            ->folder('upload/banner')
            ->save();

dd(
    $file->get(),
    $file->first(),
);

调整大小

  • 接受两个参数作为 size int 宽度和高度
    • 返回 self 的一个实例
File::name()
    ->folder('upload/banner')
    ->save(function($response){

        // perform resize
        // width, height
        $response->resize(400, 400);
    });

水印

  • 接受三个参数 watermarkSource | position | padding
    • [必填] $watermarkSource
    • 返回 self 的一个实例
    • 填充在所有位置上均匀应用,除了 center
File::name('avatar')
    ->folder('upload/banner')
    ->save(function($response){

        // perform watermark
        $response->watermark('watermark.png', 'center', 50);
    });

压缩

  • 返回 self 的一个实例
File::name('avatar')
    ->folder('upload/banner')
    ->save(function($response){

        // perform compressor
        $response->compress();

        // you can perform method chaining as well
        $response->resize(200, 450)
                    ->watermark('watermark.png', 'center')
                    ->compress();
        
    });

获取外部图像大小

  • 接受一个 string 参数
    • 返回 array | null
File::getImageSize('full_source_path')

[
  ["height"]    => int(4209)
  ["width"]     => int(3368)
]

获取上传图像大小

  • 接受一个 string 参数
    • 返回 array | null
File::imageSize('name_of_uploaded_file')

[
  ["height"]    => int(4209)
  ["width"]     => int(3368)
]

获取MIME类型

  • 接受一个 string 参数
    • 返回 string | bool。错误时返回 false
File::getMimeType('full_source_path')

非空

  • 接受一个 string 参数。输入文件名
    • 返回 bool true | false
File::notEmpty('avatar');
File::isNotEmpty('avatar');
File::has('avatar');

为空

  • 与非空相同
File::isEmpty('avatar')

有错误

  • 返回 true 或 false。检查上传中是否有错误
$file = File::name('avatar')

if($file->hasError()){

}

已完成

  • 返回 true 或 false。检查上传是否已完成
$file = File::name('avatar')

if($file->isCompleted()){

}

MIME类型

File::name('invoiceDescription')
    ->mime('zip') 
    ->save();

有用链接