定制 sar-cubet/file-upload 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sar-cubet/file-upload

最新稳定版本:v1.1.8

Composer 安装命令:

composer require sar-cubet/file-upload

包简介

An interface for users to upload different type of files and store them.

README 文档

README

License

This is a Laravel package that provides file upload functionality with ease. It simplifies the process of handling file uploads in your Laravel application.

Features

  • Simple and intuitive file upload handling.
  • Integration with Laravel's validation system.
  • Image Optimization.
  • Image Resize.
  • File Storage.
  • Chunk File Upload for large files.
  • Virus scan.

Requirements

  • PHP >= 8.0
  • Laravel >= 9.52.7

Installation

You can install the package via Composer. Run the following command:

composer require sar-cubet/file-upload

Installing the package (publishing the resources)

php artisan fileupload:install

Implementation

We provide SarCubet\FileUpload\Facades\Upload Facade which you can use to perform operations like file validation, image optimization, file storage etc. You can validate your file by using the Upload::validate() method. The Upload::validateFile() accepts a file of type Illuminate\Http\UploadedFile and return a Illuminate\Support\Facades\Validator object that you can use as per the application requirement. All the supported file types can be found inside the config file config/fileupload.php (which will be published). You can modify the config as per your requirement. Also if you wish to add any other file types, you can add it up inside allowed_file_extensions.others list in the config file.

'allowed_file_extensions' => [
    'image' => ['jpeg', 'jpg', 'png', 'gif'],
    'doc' => ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx'],
    'text' => ['txt'],
    'others' => []
]
use SarCubet\FileUpload\Facades\Upload;

$validator = Upload::validateFile($request->file('file'));
if ($validator->fails()) {
    return response()->json(['status' => 0, 'errors' => $validator->errors()]);
}

If you want to use custom validation and/or validation messages you can pass the rules and messages as an associative array (optional)

$rules = [
    'required'      => 'The file is required',
    'mimes:jpg,png' => 'The file types should be any of the following: jpg,png',
    'max:5120'      => 'The file size should not exceed 5MB'
];

$validator = Upload::validateFile($request->file('file'), $rules);

If you wish to scan the uploaded file for any type of malwares. You can use the Upload::scanFile() method(Note: Its recomented to use before using validateFile() or any other services.). Under the hood we are making use of ClamAV anti-virus scanner. So you need to install ClamAV in your machine. Here are the commands for installation (Ubuntu):

# Install clamav virus scanner
sudo apt-get update && sudo apt-get install -y clamav-daemon
# Update virus definitions
sudo freshclam
# Start the scanner service
sudo systemctl enable --now clamav-daemon clamav-freshclam

Additionally you can use isFileInfected() and getMalwareName() methods identify and provide information of any malware like below:

$scan_file = Upload::scanFile($request->file('file'));
        
if ($scan_file->isFileInfected()) {
    return "This file is found with the malware :" . $scan_file->getMalwareName() . '.';
} else {
    return "This file is safe to upload.";
}

You can optimize the image by using Upload::optimizeImage() method. Optimization is provided in 3 levels (excellent, moderate and average).

$file = Upload::optimizeImage($request->file('image'), 'moderate'); 

You can resize the image by using Upload::resize() method. The resize() method accepts 4 parameters: width, height, file and preserve_aspect_ratio (optional)

Upload::resize(200, null, $file);

or

Upload::resize(200, null, $file, true);

File storage is possible through Upload::store() method. store() accepts 3 parameters: file, disk_name, path (optional)

$url = Upload::store($file, 's3');

or

$url = Upload::store($file, 's3', 'your_path');

Chunk file upload functionlaity is provided. You can use Upload::receiveChunks() method to receive chunks and its metadata. You can pass the $request instance into the method. The function will automatically recieve the metadata and each chunks and combine the chunks into a new file when all chunks are received.

use Illuminate\Http\Request;

public function chunkFileUpload(Request $request)
{
    $recieve = Upload::receiveChunks($request);
}

The first Http POST request should contain the metadata and then the chunks should be send. The metadata should contain the data as shown below.

metadata:{
    chunk_size: chunk_size,
    total_chunk_count: total_chunk_count,
    file_size: file_size,
    file_extension: file_extension
}

You can check upload status and store the file like below.

if($receive->isUploadComplete()){
    Upload::store($receive->getFile(), 'public');
}

We also provide two methods for getting the last uploaded chunk and upload progress in percentage.

$receive->getLastUploadedChunkIndex();
$receive->getUploadProgressInPercentage();

统计信息

  • 总下载量: 45
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固