Upload file with CakePHP

3

Regarding upload file, I have the following codes up to now:

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();
?>

This is working, but I need to insert this view into 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 simply paste the view that works inside the view opening call, there is no error, but my upada image does not go to the destination folder . I think it might be something to fix in the controller of the "Add" function, its code looks 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 that if I could call the "upload" function into the "add" function, I would solve the problem, but how can I do that?

    
asked by anonymous 01.12.2015 / 18:10

1 answer

1

You should 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.

    
21.05.2016 / 15:17