Question:
Regarding the upload file, I have the following codes so far:
Controller:
public
function initialize() {
parent::initialize();
$this - > loadComponent('Upload');
}
public
function upload() {
if (!empty($this - > request - > data)) {
$this - > Upload -> send($this - > request - > data['uploadfile']);
}
}
view:
<?php echo $this->Form->create(null, ['type' => 'file']); ?>
<label>Arquivos</label>
<?php
echo $this->Form->file('uploadfile.', ['multiple']);
echo $this->Form->button('Submit', ['type' => 'submit']);
echo $this->Form->end();
?>
It's working, but I need to insert this view inside another view (in this case, inside a call opening form), which has this code:
view "add"
<?php
echo $this->Form->input('id' );
echo $this->Form->input('titulo');
echo $this->Form->input('ip');
echo $this->Form->input('mensagem');
?>
But if I just paste the view that works into the call opening view , it doesn't get any error, but my uploaded image doesn't go to the destination folder. I believe it might be something to fix in the controller of the "Add" function, its code is like this:
controller
public
function add() {
$post = $this -> Posts -> newEntity();
if ($this -> request -> is(['post', 'put'])) {
$this -> Posts -> patchEntity($post, $this - > request - > data);
$post -> user_id = $this - > Auth -> user('id');
if ($this -> Posts -> save($post)) {
$this -> Flash -> success(__('Chamado enviado, aguarde resposta... '));
return $this -> redirect(['action' => 'listar']);
}
$this -> Flash -> error(__('Chamado não enviado'));
}
$this -> set(compact('post'));
}
I think if I could call the "upload" function into the "add" function, it would solve the problem, but how can I do that?
Answer:
You must keep the ['type' => 'file'] as one of the parameters present in the create() method when creating a form that should upload files, otherwise the file will not be transferred.