maymeow/file-upload

CakePHP 的 FileUpload 插件

资助包维护!
maymeow
Ko Fi

安装次数: 1,189

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 4

分支: 0

开放问题: 0

类型:cakephp-plugin

v2.0.0 2023-12-26 15:39 UTC

README

⚠️ 这是对 2.x 版本的说明。对于 1.x 版本,请访问 v1.1.2 发布版。

🛑 破坏性变更(使用前请阅读)

2.x 版本与低于 5.x 的 cakephp 不兼容,且与该插件之前的版本不向后兼容。

  • 移除了对 S3 存储的支持(将在未来的版本中添加)
  • 添加了对 Bunny CND 存储的支持
  • 所有组件和管理器都已重写。

支持的操作是上传和下载。

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

安装 composer 包的推荐方法是

🐘 从 packagist

composer require maymeow/file-upload "^2.0.0"

用法

在您的配置文件中添加配置

'Storage' => [
    'defaultStorageType' => env('STORAGE_DEFAULT_STORAGE_TYPE', 'local'),
    'local' => [
        'managerClass' => LocalStorageManager::class,
        'storagePath' => env('STORAGE_LOCAL_STORAGE_PATH', ROOT . DS . 'storage' . DS),
    ],
    'bunny' => [
        'managerClass' => BunnyStorageManager::class,
        'cdnDomain' => env('BUNNY_STORAGE_CDN_DOMAIN', ''), // your cnd url
        'region' => env('BUNNY_STORAGE_REGION', ''), // region, empty is DE
        'baseHostName' => 'storage.bunnycdn.com', // base host name not changeable
        'storageZone' => env('BUNNY_STORAGE_ZONE', ''), // your storage zone name
        'storageZonePath' => env('BUNNY_STORAGE_ZONE_PATH', ''), // folder in zono
        'accessKey' => env('BUNNY_STORAGE_ACCESS_KEY', ''), // API key for write access
    ]
]

对于 bunny cdn 最小配置需要以下键配置

BUNNY_STORAGE_ACCESS_KEY=
BUNNY_STORAGE_CDN_DOMAIN=
BUNNY_STORAGE_ZONE=

如果您需要/想要 nginx 在不使用 PHP 的情况下提供您的静态文件,请将 STORAGE_LOCAL_STORAGE_PATH 位置设置为 webroot 文件夹。

通过添加以下内容来加载插件

$this->addPlugin('FileUpload'); // in your Application.php bootstrap function

或者您可以通过 plugins.php 配置文件添加您的插件

return [
    // .. your other plugins
    'FileUpload' => [],
];

加载组件

$config = Configure::read('Storage.local'); // or Storage.bunny

// or by setting it with .env STORAGE_DEFAULT_STORAGE_TYPE
$storageType = Configure::read('Storage.defaultStorageType');
$config = Configure::read('Storage.' . $storageType); 

$this->loadComponent('FileUpload.Upload', $config);
$this->loadComponent('FileUpload.Download', $config);

上传文件

$file = $this->Upload->getFile($this);
// do something with file

// store your file name in database
$file->getFileName(); // sanitized file name - with removed restricted characters in the name
$file = $this->Download->getFile($resource->name);
// do something with file

📝 注意,上述函数将读取文件内容,然后您需要使用响应将其推送到观众。如果您想在不使用 PHP 的情况下完成此操作,您将需要获取文件的 URL,如果使用本地存储管理器,您的文件夹需要位于 webroot 文件夹中。

$file->get('storagePath'); // local: /patht/to/sorage or bunny: https://cdn.your.tld/path/to/folder/
// combine it with filename from your database go download it

许可证

MIT