h4kuna/upload

此包已被废弃且不再维护。作者建议使用 league/flysystem 包。

上传文件的简易助手。

v0.2.5 2018-02-19 08:02 UTC

This package is auto-updated.

Last update: 2023-02-10 06:51:46 UTC


README

此包已被废弃且不再维护。作者建议使用 league/flysystem 包。

上传

Build Status Downloads this Month Scrutinizer Code Quality Latest stable Coverage Status

此扩展帮助您将上传的文件保存到文件系统并为数据库做准备。

需要 PHP 5.4+。

此扩展适用于 Nette 框架

项目安装

使用 composer 安装 h4kuna/upload 是最佳方式

$ composer require h4kuna/upload

支持

v 0.1.0 - 0.2.2 支持 php <= 5.5

入门指南

简单的注册,就像扩展一样。

extensions:
    uploadExtension: h4kuna\Upload\DI\UploadExtension

您需要创建可写目录 %wwwDir%/upload 并使用新服务。

  • uploadExtension.driver.default 对象 LocalFilesystem
  • uploadExtension.upload.default 对象 使用此驱动上传
  • uploadExtension.download.default 对象 使用此驱动下载

更多选项

在 neon 配置文件中注册 Nette 扩展。

# optional
uploadExtension:
	destinations: %wwwDir%/upload # this is default, you must create like writeable

	# or destinations can by array
	destinations:
		public: %wwwDir%/upload # first is default
		private: %appDir%/private # string or service h4kuna\Upload\IDriver

第一个目的地(例如 public)是自动注入的。为每个目的地创建 uploaddownload 服务。

手动添加依赖

services:
    - UploadControl(@uploadExtension.upload.private)
    - DownloadControl(@uploadExtension.download.private)

将上传服务注入到您的类中并使用它。返回一个对象 h4kuna\Upload\Store\File,以便稍后创建 IStoreFile

上传的简单用法

/* @var $upload h4kuna\Upload\Upload */


try {
    /* @var $file Nette\Http\FileUpload */
    
    // second parameter is optional
    $storedFile = $upload->save($file, 'path/by/id');
	

    /* @var $storedFile h4kuna\Upload\Store\File */
    $fileTable->insert([
        // required parameters for response (Download) for create IStoreFile
        'name' => $storedFile->getName(),
        'filesystem' => $storedFile->getRelativePath(),
        'type' => $storedFile->getContentType(),
        
        // optional
        'size' => $file->getSize(),
        'is_image' => $storedFile->isImage
    ]);
} catch (\h4kuna\Upload\FileUploadFailedException $e) {
	// upload is failed
}

要创建更多上传选项,请创建对象 h4kuna\Upload\Upload\Options

如果已定义过滤器,则可以预期出现新的异常 UnSupportedFileTypeException。这两个异常都有相同的父类 UploadException

$filter = h4kuna\Upload\Upload\FilterFactory::createImage();
$name = new h4kuna\Upload\Upload\Filename(); // default
try {
    $storedFile = $upload->save($file, new h4kuna\Upload\Upload\Options('subdir/by/id', function(h4kuna\Upload\Store\File $storedFile, Nette\Http\FileUpload $uploadFile) {
        // if you need add optional property like size, etc. 
        $storedFile->isImage = $uploadFile->isImage();
        
        // Custom rule: allow image bigger then 500x500px
        if ($storedFile->isImage) {
            $image = $uploadFile->getImage();
            if($image->getHeight() < 500 || $image->getWidth() < 500) {
                return false; // this return throw exception
            }
        }
    
    }, $name, $filter));
} catch (FileUploadFailedException $e) {
    // driver is failed
} catch (UnSupportedFileTypeException $e) {
    // unsupported file, filter refused
}

这里准备了对象 h4kuna\Upload\UploadSpecific

现在创建 Nette\Application\Responses\FileResponse 用于下载文件。

如果您在 presenter 中使用

/* @var $download h4kuna\Upload\Download */

// this create from data whose stored in database
$file = new File(...); // instance of IStoreFile
$presenter->sendResponse($download->createFileResponse($file));

或者如果您使用自己的脚本

$file = new File(...); // instance of IStoreFile

try {
	$download->send($file);
} catch (\h4kuna\Upload\FileDownloadFailedException $e) {
	$myResponse->setCode(Nette\Http\IResponse::S404_NOT_FOUND);
}
exit;

FTP

FTP 客户端是 dg/ftp-php,但未包含在此 composer 中。包装器是 Ftp

uploadExtension:
    ftp:
        test:
            host: example.com
            user: root
            password: 123456
            url: http://example.com/images
            # optional
            path: images
            port: 21
            passive: true

这会创建服务

  • uploadExtension.ftp.test 客户端 \Ftp
  • uploadExtension.driver.test IDriver
  • uploadExtension.upload.test 使用此驱动器上传
  • uploadExtension.download.test 使用此驱动器下载

自定义驱动器服务

该库仅在文件系统和 ftp 上保存文件。如果您需要通过 ssh 等方式在远程服务器上保存,目前尚未实现,但已做好准备。让我们以 LocalFilesystem 为例

  • 使用 IDriver 接口创建自己的类
<?php

class SshDriver implements \h4kuna\Upload\IDriver
{
// your implementation
} 
  • 如有需要,请注册 neon
services:
    mySshDriver: SshDriver
    
    uploadComponent: UploadControl(@uploadExtension.upload.sshDriver)
    reponseComponent: DownloadControl(@uploadExtension.download.sshDriver)
    
uploadExtension:
	destinations:
		public: %wwwDir%/upload # first is default  
		sshDriver: @mySshDriver