peterson / ultimate-uploader
支持所有文件类型的PHP上传库
2.2.3
2023-08-25 20:05 UTC
Requires
- php: >=5.3.3
README
@作者 Fredrick Peterson (Tame Developers)
PHP Ultimate Image Uploader Library
|--------------------------------------------------------------------------
|This Library uses Image Compressor by can Bachors
|https://github.com/bachors/PHP-Image-Compressor-Class
需求
>= php5.3.3+
安装
在安装 ultimate-uploader
之前,获取PHP依赖管理器 Composer,因为它将简化安装过程。
第一步 — 更新你的 composer.json
"require": { "peterson/ultimate-uploader": "^2.2.3" }
或者 composer install:
composer require peterson/ultimate-uploader
第二步 — 运行 Composer
composer update
实例化
第一步 — 使用Composer 实例化类
require_once __DIR__ . '/vendor/autoload.php';
$upload = new ultimateUploader\ultimateUploader();
or
require_once __DIR__ . '/vendor/autoload.php';
use \ultimateUploader\ultimateUploader;
$upload = new ultimateUploader();
第二步 — 使用PHP直接 实例化类
include_once "pat_to/ultimateUploader.php";
$upload = new ultimateUploader\ultimateUploader();
You can download and entire repo and copy the src file alone to directory of your project.
- src/ultimateUploader.php
设置目录路径
第一步:
$upload = new ultimateUploader\ultimateUploader();
$upload->setDir('path-to-dir'):
第二步 :
$upload = new ultimateUploader\ultimateUploader();
$upload->setDirectory('path-to-dir'):
设置URL路径
第一步:
$upload = new ultimateUploader\ultimateUploader();
$upload->setUrl('path-to-url'):
第二步:
$upload = new ultimateUploader\ultimateUploader();
$upload->setURL('path-to-url'):
错误代码
|--------------------------------------------------------------------------
| ALL Error Code
| Some error can be skipped, depending on what you need
-> ERROR_400 - no file upload
-> ERROR_401 - select file to upload
-> ERROR_402 - File upload size is bigger than allowed size limit
-> ERROR_403 - Maximum file allowed exceeded
-> ERROR_404 - Uploaded file format not allowed
-> ERROR_404 - Image size allowed error
-> ERROR_500 - Input file `name[]` must be passed as an array
| *************************************
所有错误代码 — 响应属性(公共)->allError
$upload = new ultimateUploader\ultimateUploader();
echo $upload->allError;
默认情况下,在调用时(实例化)类有三个(3)参数
-> errorDisallowed -- index array [], all error code to ignore
-> base_dir -- Path to base dir (NULL on default)
-> base_url -- Path to base storage url (NULL on default)
$upload = new ultimateUploader\ultimateUploader([400, 401], 'path_to_dir', 'localhost/path_to_storage/folder');
- By default the error disallow array is empty
- By default the base dir, get path to your document root base directory
- By default the base url, get path to your base domain url/link
当从类中调用 run 方法时,我们有七个(7)个参数
-> fileUpload -- HTML input file name
-> folder_create -- Folder structure to use (Refer to Folder Create section for more detail)
-> upload_dir -- Directory to upload to (Do not pass in full path to folder)
-> type -- Mime Type to allow (Refer to MIME Types section for more detail)
-> size -- Maximum file size to allow (use any value of choice without conversion) i.e 2mb
-> limit_max -- Maximum upload limits
-> dimension_size -- For image dimension sizes (espects associative array). Please refer to DIMENSION PARAM ERROR
输入HTML结构
<input type="file" name="avatar[]">
用例定义
** -> We used Object Method Chaining **
** -> $upload->run()->error()->success(); **
** -> a new callable function is espected to be passed to "error Method and success Method" **
** -> pass any param `name` of choice on error and success function call **
- 普通默认设置
$upload = new ultimateUploader\ultimateUploader();
$upload->run('avatar', 'default', 'upload/avatar', 'images', '1.5mb', 1, ['height' => 500, 'width' => '300'])
->error(function($error){
//you now have access to all public methods & properties using the $error var
})->success(function($success){
//you now have access to all public methods & properties using the $success var
});
- 示例1
//instantiate class
$upload = new ultimateUploader\ultimateUploader();
$upload->run('avatar', 'year', 'upload/avatar', 'images', '1.5mb', 1)
->error(function($response){
//error message
echo $response->data['message'];
})->success(function($response){
On Successful uploads --- ERROR_200
//run auto resize
$response->imageAutoResize(200, 100, false);
//run watermark
$response->waterMark('watermark.png', '50', '100', false); //Add watermark automatically
//run compression
$response->compress(true); //will replace original to compressred -v
//->data properties contains all uploaded info
$response->data;
});
- 示例2
//instantiate class
$upload = new ultimateUploader\ultimateUploader([400]);
$upload->run('avatar', 'month', 'upload', 'images', '2.5mb', 2)
->error(function($response){
//you can customize each error text message
if($response->data['status'] == 401){
echo "Custom message - Please select a file to upload";
return; //return key is used to stop further code exec on this function block only
}
//error message
echo $response->data['message'];
})->success(function($response){
//proccess further code blocks
$response->data;
});
- 示例3
//instantiate class
$upload = new ultimateUploader\ultimateUploader([400]);
$upload->run('avatar', 'default', 'upload/avatar', 'images', '1.5mb', 1, ['height' => 500, 'width' => '300'])
->error(function($response){
//you can customize each error text message
if($response->data['status'] == 401){
echo "Custom message - Please select a file to upload";
}
elseif($response->data['status'] == 405){
echo "CUSTOM MSG - {$response->data['message']}";
return;
}
//error message
echo $response->data['message'];
})->success(function($response){
//proccess further code blocks
$response->data;
});
第一
$upload = new ultimateUploader\ultimateUploader();
->success(function($response){
$response->first();
This will get all first uploaded data
returns as an array
});
获取
$upload = new ultimateUploader\ultimateUploader();
->success(function($response){
$response->get();
This will get all uploaded data
returns as an array
});
获取参数大小
Dimension size error check on $upload->run();
-> Takes an associative array --- ['width' => 500, 'height' => 700, 'same' => false]
-> By default same is set to `false` if not set.
$upload->run(['width' => 500, 'height' => 700])
['same' => false] => "Will only check if height or weight is greater or equal to allowed dimension set"
['same' => true] => "Will only check if height or weight is equal to allowed dimension set"
获取图片宽度和高度
Must be called before the ->run Method for this to work
Useful for a single file upload like Cover Image or any other single upload
--- Takes just one param (HTML input file name)
//get file data
$width = $upload->getImageAttribute('path_to_img_file);
var_dump($width);
--- returns an assoc array
[
["height"]=> int(4209)
["width"]=> int(3368)
]
图片自动调整大小
By default Image Autoresize is set to -> false
You need to set to -> true in other to enable the method
-- Autoresize takes the lowest length value between Width & Height to crop/resize image
$upload->imageAutoResize(200, 100, false);
图片水印
By default Watermark is set to -> false
You need to set to -> true in other to enable the method
-- Watermark takes first param as path to image
i.e 'assets/image/watermark.png'
-- Second param is margin_right
-- third param if margin_bottom
$upload->waterMark('watermark.png', '50', '100', true);
图片压缩
By default Compressor is set to -> false
You need to set to -> true in other to enable the method
Best practice for compressing is to make this the last callback method after the rest
->imageAutoResize
->waterMark
->compress
$upload->compress(true);
创建文件夹
By default Folder structure parameter is set to -> 'default'
-- Do not worry as folder do not need to exists before it can be created.
-- All uploaded files return all dataset of all uploads
-> 'default' Upload directory name/filename.jpg
-> 'year' Upload directory name/2022/filename.jpg
-> 'month' Upload directory name/2022/10/filename.jpg
-> 'day' Upload directory name/2022/10/28/filename.jpg
- 默认
- 年份
- 月份
- 日期
- 月份
- 年份
To use, pass in any of the below;
'default' | 'year' | 'month' | 'day' to the run method when calling
$upload->run('avatar', 'year', 'upload/avatar', 'images', '1.5mb', 1);
MIME类型
'video' => ['.mp4', '.mpeg', '.mov', '.avi', '.wmv'],
'audio' => ['.mp3', '.wav'],
'files' => ['.docx', '.pdf', '.txt'],
'images' => ['.jpg', '.jpeg', '.png'],
'general_file' => ['.docx', '.pdf', '.txt', '.zip', '.rar', '.xlsx', '.xls'],
'general_image' => ['.jpg', '.jpeg', '.png', '.webp'],
'general_media' => ['.mp3', '.wav', '.mp4', '.mpeg', '.mov', '.avi', '.wmv']
Pass in any of this into the Type parameter section when calling the ->run Method
- 视频
- 音频
- 文件
- 通用文件
- 图片
- 通用图片
- 通用媒体
最终上传数据
Array
(
[status] => 200
[message] => avatar Uploaded successfully
[file] => Array
(
[image] => Array
(
[0] => path_to_uploaded_file.jpeg
)
[new_image] => Array
(
[0] => 164471880099d95c853c830f6.jpeg
)
[folder] => Array
(
[0] => upload/164471880099d95c853c830f6.jpeg
)
[folder_real_path] => Array
(
[0] => C:/xampp/htdocs/ultimateUploader-main/upload/164471880099d95c853c830f6.jpeg
)
[folder_url] => Array
(
[0] => https:///ultimateUploader-main/upload/164471880099d95c853c830f6.jpeg
)
)
[ext] => Array
(
[0] => jpeg
)
)
示例图片
示例图片原始
Image size of 2.2mb
示例图片原始 - 压缩 -v
Image compressed to 988kb
Watermarked and retain its original quality
有用的链接
- 如果您喜欢这个PHP库,可以为Tame Developers买杯咖啡
- 关于使用方法的YouTube视频教程将很快提供