Upload file com CakePHP


Regarding the upload file, I have the following codes so far:


 function initialize()  {
     $this - > loadComponent('Upload');

 function upload() {
     if (!empty($this - > request - > data)) {
         $this - > Upload -> send($this - > request - > data['uploadfile']); 


<?php echo $this->Form->create(null, ['type' => 'file']); ?>
    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"

    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:


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?


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.

Scroll to Top