Skip to content

Commit 6e8e801

Browse files
committed
update readme
1 parent 8e81855 commit 6e8e801

File tree

1 file changed

+10
-143
lines changed

1 file changed

+10
-143
lines changed

README.md

Lines changed: 10 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# 🆙 FileUpload plugin for CakePHP
22

3+
:warning: This is readme for version 2.x. For 1.x go to [v1.1.2](https://github.com/MayMeow/cakephp-fileupload/tree/v1.1.2) release.
4+
5+
### :stop_sign: Breaking changes (read before use)
6+
7+
Version 2.x is not compatibile with cakephp lower than 5.x and it is not backward compatibile with previous releases of this plugins.
8+
9+
- S3 storage support was removed
10+
- Bunny CND storage support was added
11+
- All components and Managers was rewritten.
12+
313
Cakephp plugin to upload files to storage and download them. Currently are supported:
414

515
- Local storage
@@ -19,149 +29,6 @@ The recommended way to install composer packages is:
1929
composer require maymeow/file-upload
2030
```
2131

22-
## Usage
23-
24-
To load plugin addd to `Application.php`
25-
26-
```php
27-
$this->addPlugin('FileUpload');
28-
```
29-
30-
Next load it in controllers where you want to use it. To use default config
31-
32-
```php
33-
public function initialize(): void
34-
{
35-
parent::initialize();
36-
$this->loadComponent('FileUpload.Upload');
37-
$this->loadComponent('FileUpload.Dowmload'); // in case you wan to download files
38-
}
39-
```
40-
41-
## Using local storage
42-
43-
Commands above are loaded with default configuration:
44-
- Using Local filesystem: type is set to `local`
45-
- Data are stored in `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 files
46-
- Field name for uploading files is `uploaded_file`
47-
- Allowed are all file types
48-
49-
```php
50-
public function initialize(): void
51-
{
52-
parent::initialize();
53-
$this->loadComponent('FileUpload.Upload', [
54-
'fieldName' => 'your_form_file_field',
55-
'storagePath' => 'path_to_storage',
56-
'allowedFileTypes' => '*'
57-
]);
58-
}
59-
```
60-
61-
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`.
62-
63-
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.
64-
65-
## Using S3 storage
66-
67-
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
68-
69-
```php
70-
public function initialize(): void
71-
{
72-
parent::initialize();
73-
$this->loadComponent('FileUpload.Upload', [
74-
'fieldName' => 'your_form_file_field',
75-
'storagePath' => 'bucket_name',
76-
'storage_type' => 's3'
77-
]);
78-
}
79-
```
80-
81-
:exclamation: Dont forget to change configuration for `DownloadComponent` too if you planing using it.
82-
83-
Next add configuration for s3 server to your config file `app_local.php` for example as follows:
84-
85-
```php
86-
'S3' => [
87-
'version' => 'latest',
88-
'region' => 'us-east-1',
89-
'endpoint' => 'http://cake_minio:9000',
90-
'use_path_style_endpoint' => true,
91-
'credentials' => [
92-
'key' => 'minioadmin',
93-
'secret' => 'minioadmin',
94-
],
95-
]
96-
```
97-
98-
Config above is for using minio with my [CakePHP starte kit](https://github.com/MayMeow/cakephp-starter-kit). Change it for your needs.
99-
100-
101-
## Uploading files
102-
103-
104-
```php
105-
/**
106-
* @throws \HttpException
107-
* @return \Cake\Http\Response|null|void
108-
*/
109-
public function upload()
110-
{
111-
$uploadForm = new UploadForm();
112-
113-
if ($this->request->is('post')) {
114-
$uploadedFile = $this->Upload->getFile($this->request);
115-
116-
// Create new Entity and store info about uploaded file
117-
$file = $this->Files->newEmptyEntity();
118-
119-
$file->name = $uploadedFile->getFileName();
120-
$file->path = $uploadedFile->getPath();
121-
122-
$this->Files->save($file);
123-
124-
return $this->redirect($this->referer());
125-
}
126-
127-
$this->set(compact('uploadForm'));
128-
}
129-
```
130-
131-
## Downloading files
132-
133-
134-
```php
135-
/**
136-
* @param string $fileName
137-
* @return \Cake\Http\Response|void
138-
*/
139-
public function download($fileName)
140-
{
141-
$downloadedFile = $this->Download->getFile($fileName);
142-
143-
$response = $this->response;
144-
$response = $response->withStringBody($downloadedFile->getFileContent());
145-
$response = $response->withType($downloadedFile->getFileType());
146-
147-
if ($this->request->getParam('?.download') == true) {
148-
$response = $response->withDownload($fileName);
149-
}
150-
151-
return $response;
152-
}
153-
```
154-
155-
## 🎯 Direction
156-
157-
* [x] Configurable field name
158-
* [x] Configurable path to storage
159-
* [x] Allowed file types
160-
* [x] Add S3 Support
161-
* [ ] Multiple file upload
162-
* [ ] File Size
163-
164-
💡 If you have more ideas then you can post issue on porect's GitHub page.
16532

16633
## License
16734

0 commit comments

Comments
 (0)