Cakephp plugin to upload files to storage and download them. Currently are supported:
And supported actions are upload and download.
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require maymeow/file-upload
To load plugin addd to Application.php
$this->addPlugin('FileUpload');
Next load it in controllers where you want to use it. To use default config
public function initialize(): void
{
parent::initialize();
$this->loadComponent('FileUpload.Upload');
$this->loadComponent('FileUpload.Dowmload'); // in case you wan to download files
}
Commands above are loaded with default configuration:
local
sotrage
folder in root of your application - This folder is not public and cannot be served directly from webserver. You need to use DownloadComponent
to get filesuploaded_file
public function initialize(): void
{
parent::initialize();
$this->loadComponent('FileUpload.Upload', [
'fieldName' => 'your_form_file_field',
'storagePath' => 'path_to_storage',
'allowedFileTypes' => '*'
]);
}
For allowedFileTypes
use 'allowedFileTypes' => '*'
for all file types or 'allowedFileTypes' => ['type1', 'type2']
for your expected file types. If file have not allowed type Component will throw Cake\Http\Exception\HttpException
.
Using local storage (default option) you can use default config but do not forge to change allowed file types and form field nam from which you uploading file to server.
For S3 storage configuration is pretty same as configuration above, but you have to change type to s3
instead of local
which is default as follows
public function initialize(): void
{
parent::initialize();
$this->loadComponent('FileUpload.Upload', [
'fieldName' => 'your_form_file_field',
'storagePath' => 'bucket_name',
'storage_type' => 's3'
]);
}
Dont forget to change configuration for
DownloadComponent
too if you planing using it.
Next add configuration for s3 server to your config file app_local.php
for example as follows:
'S3' => [
'version' => 'latest',
'region' => 'us-east-1',
'endpoint' => 'http://cake_minio:9000',
'use_path_style_endpoint' => true,
'credentials' => [
'key' => 'minioadmin',
'secret' => 'minioadmin',
],
]
Config above is for using minio with my CakePHP starte kit. Change it for your needs.
/**
* @throws \HttpException
* @return \Cake\Http\Response|null|void
*/
public function upload()
{
$uploadForm = new UploadForm();
if ($this->request->is('post')) {
$uploadedFile = $this->Upload->getFile($this->request);
// Create new Entity and store info about uploaded file
$file = $this->Files->newEmptyEntity();
$file->name = $uploadedFile->getFileName();
$file->path = $uploadedFile->getPath();
$this->Files->save($file);
return $this->redirect($this->referer());
}
$this->set(compact('uploadForm'));
}
/**
* @param string $fileName
* @return \Cake\Http\Response|void
*/
public function download($fileName)
{
$downloadedFile = $this->Download->getFile($fileName);
$response = $this->response;
$response = $response->withStringBody($downloadedFile->getFileContent());
$response = $response->withType($downloadedFile->getFileType());
if ($this->request->getParam('?.download') == true) {
$response = $response->withDownload($fileName);
}
return $response;
}
💡 If you have more ideas then you can post issue on porect's GitHub page.
MIT