tamedevelopers / file
PHP文件上传库
3.0
2024-07-28 04:58 UTC
Requires
- php: >=8.0
- tamedevelopers/support: *
- tamedevelopers/validator: *
Suggests
- aws/aws-sdk-php: Required to use Amazon `s3` composer require aws/aws-sdk-php (^3.282.0).
README
文档
需求
>= 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
网站。
- 最佳用于
- 默认值
- 年
- 月
- 日
- 月
- 年
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
|int
和bool
- 第一个参数是
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
- 返回 bool
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();
有用链接
- @作者 Fredrick Peterson (Tame Developers)
- 如果你喜欢这个 PHP 库,你可以 为 Tame Developers 购买咖啡