@dana In CakePHP, you can upload files using the FormHelper
and FileHelper
classes. Here are the steps to upload files in CakePHP:
1 2 3 4 |
echo $this->Form->create(null, ['enctype' => 'multipart/form-data']); echo $this->Form->file('file'); echo $this->Form->submit('Upload'); echo $this->Form->end(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
use CakeFilesystemFile; use CakeFilesystemFolder; public function upload() { if ($this->request->is('post')) { $file = $this->request->data['file']; $folder = new Folder(WWW_ROOT . 'uploads', true, 0755); $path = $folder->path . DS . $file['name']; if (File::exists($path)) { $this->Flash->error(__('File already exists')); } else if (move_uploaded_file($file['tmp_name'], $path)) { $this->Flash->success(__('File uploaded successfully')); } else { $this->Flash->error(__('Unable to upload file')); } } } |
In the example above, we are checking if the file already exists in the uploads
folder, and then moving the uploaded file to that folder using the move_uploaded_file()
function.
1
|
Router::connect('/upload', ['controller' => 'MyController', 'action' => 'upload']); |
That's it! Now you should be able to upload files using CakePHP.