Issue
I was following this guide to do uploads on CakePHP 3. Using that guide, I select a file, and when I click upload, it moves that file to a designated folder inside the project, sets that folder as the path, and creates a newEntity in the database.
I'm trying to now get this same upload to be able to overwrite an existing entity (i.e. Edit instead of Add).
I duplicated the Upload function in the controller and View and modified them to be used as an Edit. However, upon attempting it, the Edit fails and I do not even get to an error message; the page just refreshes as if nothing has occurred.
The relevant table is Files, which has the following attributes:
- ID (primary key)
- link (the folder where the uploaded files are kept)
- name (the name of the file)
- created
- modified
This is my function in the controller:
public function edit($id = null)
{
$uploadData = $this->Files->get($id, [
'contain' => []
]);
if ($this->request->is('post')) {
$data = $this->request->data;
if(!empty($data['link']['name'])){
$fileName = $data['link']['name'];
$uploadPath = 'uploads/template/';
$uploadFile = $uploadPath.$fileName;
if(move_uploaded_file($data['link']['tmp_name'],$uploadFile)){
$uploadData = $this->Files->patchEntity($uploadData, $data);
$uploadData->name = $fileName;
$uploadData->link = $uploadPath;
if ($this->Files->save($uploadData)) {
$this->Flash->success(__('File has been uploaded and updated successfully.'));
}else{
$this->Flash->error(__('Unable to upload file, please try again.'));
}
}else{
$this->Flash->error(__('Unable to upload file, please try again.'));
}
}else{
$this->Flash->error(__('Please choose a file to upload.'));
}
}
$files = $this->Files->find('all', ['order' => ['Files.created' => 'DESC']]);
$filesRowNum = $files->count();
$blanks = $this->Files->find('all', ['order' => ['Files.created' => 'DESC']])->first();
$this->set(compact('blanks', 'uploadData', 'files', 'filesRowNum'));
}
And this is my form:
<?php echo $this->Form->create($uploadData, ['type' => 'file']); ?>
<?php echo $this->Form->input('link', ['type' => 'file', 'class' => 'form-control', 'label' => 'Template Link' ]); ?><br>
<?php echo $this->Form->button(__('Upload File'), ['type'=>'submit', 'class' => 'btn btn-primary', 'style' => 'width:25%;']); ?>
<?php echo $this->Form->end(); ?>
In the controller, I've also attempted to replace if ($this->request->is('post')) {
with ($this->request->is('patch', 'post', 'put')) {
, similar to the Cake baked Edit pages.
If say I was to upload a blank Excel spreadsheet called Book 2.xlsx
and wanted to overwrite the 4th data entry in the Files table, I would go to the URL localhost/project/files/edit/4
, select the file and submit.
Upon doing so, no error messages appear, either Cake errors or flash errors, and in the CakePHP Request, I have the following array in POST:
link (array)
name: Book2.xlsx
type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
tmp_name: C:\xampp\tmp\php3B10.tmp
error: 0
size: 7823
Solution
Change this:
if ($this->request->is('post')) {
To this:
if ($this->request->is('post') || $this->request->is('put')) {
Edit: As ndm pointed out, arrays work too:
$this->request->is(['post', 'put', 'patch'])
Answered By - Marijan
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.