You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 5, 2022. It is now read-only.
[Make sure your form is using the right HTTP method](http://book.cakephp.org/3.0/en/views/helpers/form.html#changing-the-http-method-for-a-form)!
42
44
43
-
**Now comes the crucial point of the whole implementation**
44
-
45
-
Because of to many different requirements and personal preferences out there the plugin is *not* automatically storing the file. You'll have to customize it a little but its just a matter for a few lines.
46
-
47
-
Lets go by this scenario inside the report model, assuming there is an add() method:
48
-
49
-
```php
50
-
$entity = $this->newEntity($postData);
51
-
$saved = $this->save($entity);
52
-
if ($saved) {
53
-
$key = 'your-file-name';
54
-
if (StorageManager::adapter('Local')->write($key, file_get_contents($this->data['pdf_files']['file']['tmp_name']))) {
Later, when you want to delete the file, for example in the beforeDelete() or afterDelete() callback of your Report model, you'll know the adapter you have used to store the attached PdfFile and can get an instance of this adapter configuration using the StorageManager. By having the path or key available you can then simply call:
Insted of doing all of this in the model that has the files associated to it you can also simply extend the FileStorage model from the plugin and add your storage logic there and use that model for your association.
71
-
72
-
How to store an uploaded file II - using Events
73
-
-----------------------------------------------
45
+
Store an uploaded file using Events
46
+
-----------------------------------
74
47
75
48
The **FileStorage** plugin comes with a class that acts just as a listener to some of the events in this plugin. Take a look at [LocalImageProcessingLister.php](../../Event/LocalImageProcessingLister.php).
76
49
@@ -105,6 +78,42 @@ Event Listeners
105
78
106
79
See [this page](Included-Event-Listeners.md) for the event listeners that are included in the plugin.
107
80
81
+
82
+
Handling the File Upload Manually
83
+
---------------------------------
84
+
85
+
You'll have to customize it a little but its just a matter for a few lines.
86
+
87
+
Note the Listener expects a request data key `file` present in the form, so use `echo $this->Form->input('file');` to allow the Marshaller pick the right data from the uploaded file.
88
+
89
+
Lets go by this scenario inside the report table, assuming there is an add() method:
90
+
91
+
```php
92
+
public function add() {
93
+
$entity = $this->newEntity($postData);
94
+
$saved = $this->save($entity);
95
+
if ($saved) {
96
+
$key = 'your-file-name';
97
+
if (StorageManager::adapter('Local')->write($key, file_get_contents($this->data['pdf_files']['file']['tmp_name']))) {
Later, when you want to delete the file, for example in the beforeDelete() or afterDelete() callback of your Report model, you'll know the adapter you have used to store the attached PdfFile and can get an instance of this adapter configuration using the StorageManager. By having the path or key available you can then simply call:
Insted of doing all of this in the table object that has the files associated to it you can also simply extend the FileStorage table from the plugin and add your storage logic there and use that table for your association.
0 commit comments