How to call a function in CakePHP 3.x [closed]

4

I have a add view which is a form called help desk (referring to the function add of course), and I have this upload which is to attach file to the request, which is linked with a component , but I do not want to leave them in different view , as if I simply send a file through the upload view, the image "gets lost" and does not connect to the call. I need the add function to be able to call the upload function to merge the views. If I call it through $ this-> upload (); , or simply do all the checking inside add , the component , returning me an error (which I'll put below), I believe the conflict is in request-> date , but I do not know if there is a way of putting it together in the way I explained.

    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'));
            return $this->redirect(['action' => 'listar']);
        }

        $this->Flash->error(__('Chamado nao enviado'));
    }


        $this->set(compact('post'));

}
public function upload()

{

    if ( !empty( $this->request->data ) ) {
        $this->Upload->send($this->request->data(['uploadfile']));
        return $this->redirect(['action'=>'add']);
    }

}

Component:

 public function send( $data )
{
    if ( !empty( $data) ) {
        if ( count( $data) > $this->max_files ) {
            throw new InternalErrorException("Error Processing Request. Max number files accepted is {$this->max_files}", 1);
        }

        foreach ($data as $file) {
            $filename = $file['name']; //linha 32, que da o erro abaixo;
            $file_tmp_name = $file['tmp_name']; //33
            $dir = WWW_ROOT.'img'.DS.'Anexos';
            $allowed = array('png', 'jpg', 'jpeg');
            if ( !in_array( substr( strrchr( $filename , '.') , 1 ) , $allowed) ) {
                throw new InternalErrorException("Error Processing Request.", 1);
            }elseif( is_uploaded_file( $file_tmp_name ) ){
                $filename = Text::uuid().'-'.$filename;

                $filedb = TableRegistry::get('Arquivos');
                $entity = $filedb->newEntity();
                $entity->filename = $filename;
                $filedb->save($entity);

                move_uploaded_file($file_tmp_name, $dir.DS.$filename);
            }
        }
    }
}

error when calling upload ();

Warning (2): Illegal string offset 'name' [APP/Controller\Component\UploadComponent.php, line 32]

Warning (2): Illegal string offset 'tmp_name' [APP/Controller\Component\UploadComponent.php, line 33]

view:

  <?php
        //essa é minha view add;
        echo $this->Form->input('id' );
        echo $this->Form->input('titulo');
        echo $this->Form->input('ip');
        echo $this->Form->input('mensagem');

    ?>
       //e essa é minha view upload, que eu gostaria de juntar com add;

      <?php echo $this->Form->create(null, ['type' => 'file']); ?>
      <label>Arquivos</label>
      <?php
      echo $this->Form->file('uploadfile.', ['multiple']);
      echo $this->Form->button('Anexar', ['action' => 'submit']);
      echo $this->Form->end();

       ?>

) gives the error I mentioned above. I can put a link directing to attach a file in another view , right then, saved in the folder and the bank, but the file is lost, the request is not linked, and it is not possible to cake do not let me do that.

    
asked by anonymous 11.12.2015 / 14:44

0 answers