sar-cubet / file-upload
一个用户界面,用于上传不同类型的文件并将它们存储起来。
Requires
- intervention/image: ^2.7
README
这是一个提供文件上传功能的Laravel包,使用起来非常简单。它简化了在Laravel应用程序中处理文件上传的过程。
特性
- 简单直观的文件上传处理。
- 与Laravel验证系统的集成。
- 图像优化。
- 图像缩放。
- 文件存储。
- 大文件分块上传。
- 病毒扫描。
要求
- PHP >= 8.0
- Laravel >= 9.52.7
安装
您可以通过Composer安装此包。运行以下命令:
composer require sar-cubet/file-upload
安装包(发布资源)
php artisan fileupload:install
实现
我们提供了SarCubet\FileUpload\Facades\Upload
外观,您可以使用它执行文件验证、图像优化、文件存储等操作。您可以使用Upload::validate()
方法验证您的文件。Upload::validateFile()
接受一个类型为Illuminate\Http\UploadedFile
的文件,并返回一个Illuminate\Support\Facades\Validator
对象,您可以根据应用程序的要求使用它。所有支持的文件类型都可以在配置文件config/fileupload.php(将发布)中找到。您可以根据需要修改配置。如果您希望添加其他文件类型,您可以在配置文件的allowed_file_extensions.others列表中添加它。
'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()]); }
如果您想使用自定义验证和/或验证消息,您可以将规则和消息作为关联数组传递(可选)
$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);
如果您希望扫描上传的文件以检测任何类型的恶意软件。您可以使用Upload::scanFile()
方法(注意:建议在使用validateFile()
或其他服务之前使用)。底层我们使用的是ClamAV反病毒扫描器。因此,您需要在您的机器上安装ClamAV。以下是安装命令(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
此外,您还可以使用isFileInfected()
和getMalwareName()
方法识别并提供任何恶意软件的信息,如下所示:
$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."; }
您可以使用Upload::optimizeImage()
方法优化图像。优化提供3个级别(优秀、适中、平均)。
$file = Upload::optimizeImage($request->file('image'), 'moderate');
您可以使用Upload::resize()
方法缩放图像。resize()
方法接受4个参数:宽度、高度、文件和preserve_aspect_ratio(可选)
Upload::resize(200, null, $file);
或者
Upload::resize(200, null, $file, true);
您可以通过Upload::store()
方法进行文件存储。store()
接受3个参数:文件、磁盘名称、路径(可选)
$url = Upload::store($file, 's3');
或者
$url = Upload::store($file, 's3', 'your_path');
提供分块文件上传功能。您可以使用Upload::receiveChunks()
方法接收块及其元数据。您可以将$request
实例传递给该方法。该函数将自动接收元数据和每个块,并在所有块都接收后组合块到一个新文件中。
use Illuminate\Http\Request; public function chunkFileUpload(Request $request) { $recieve = Upload::receiveChunks($request); }
第一个HTTP POST请求应包含元数据,然后发送块。元数据应包含如下所示的数据。
metadata:{ chunk_size: chunk_size, total_chunk_count: total_chunk_count, file_size: file_size, file_extension: file_extension }
您可以检查上传状态并如下存储文件。
if($receive->isUploadComplete()){ Upload::store($receive->getFile(), 'public'); }
我们还提供了两种方法来获取最后上传的块和上传进度百分比。
$receive->getLastUploadedChunkIndex(); $receive->getUploadProgressInPercentage();