承接 hosametm/file-scanning 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

hosametm/file-scanning

最新稳定版本:v1.0.1

Composer 安装命令:

composer require hosametm/file-scanning

包简介

A Laravel package for secure file scanning and validation

README 文档

README

A Laravel package for secure file scanning and validation. This package helps you validate files based on their MIME types and provides protection against potentially malicious file uploads.

Installation

You can install the package via composer:

composer require hosametm/file-scanning

After installing the package, publish the configuration file:

php artisan vendor:publish --tag=file-scanning-config

Configuration

The configuration file (config/file-scanning.php) contains three main sections:

  1. temp_directory: The directory where temporary files will be stored when downloading files from URLs (defaults to system temp directory)
  2. mime_types: A list of allowed file extensions and their corresponding MIME types
  3. malicious_mime_types: A list of MIME types that are considered potentially malicious

You can customize these settings according to your needs.

Usage

Basic Usage with File Uploads

use Hosametm\FileScanning\FileScanner;

class YourController extends Controller
{
    protected $fileScanner;

    public function __construct(FileScanner $fileScanner)
    {
        $this->fileScanner = $fileScanner;
    }

    public function upload(Request $request)
    {
        $file = $request->file('file');
        
        if ($this->fileScanner->validateUpload($file)) {
            // File is valid, proceed with upload
        } else {
            // File is invalid or potentially malicious
        }
    }
}

Validating Files from URLs

use Hosametm\FileScanning\FileScanner;

class YourController extends Controller
{
    protected $fileScanner;

    public function __construct(FileScanner $fileScanner)
    {
        $this->fileScanner = $fileScanner;
    }

    public function validateUrl(Request $request)
    {
        $url = $request->input('file_url');
        
        if ($this->fileScanner->validateUrl($url)) {
            // File from URL is valid, proceed with processing
        } else {
            // File is invalid or potentially malicious
        }
    }
}

Available Methods

Validate a File Path

$fileScanner->validate(string $filePath): bool

Validate an Uploaded File

$fileScanner->validateUpload(\Illuminate\Http\UploadedFile $file): bool

Validate a File from URL

$fileScanner->validateUrl(string $url): bool

Get File MIME Type

$fileScanner->getMimeType(string $filePath): ?string

Check if File is Malicious

$fileScanner->isMalicious(string $filePath): bool

Get Extensions from MIME Type

$fileScanner->getExtensionsFromMimeType(string $mimeType): array

Security

This package helps protect your application by:

  • Validating file MIME types
  • Blocking potentially malicious file types
  • Providing a configurable whitelist of allowed file types
  • Securely handling file uploads and URL downloads
  • Automatically cleaning up temporary files

Contributing

Please see CONTRIBUTING.md for details.

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-12-23