1
votes

I have a view that is add a called help desk opening form (referring to add function, of course), and have this function is to upload that file to attach request, which is connected with a component, but I do not want to Let them in different view, because if I just send a file upload through the view, the image is "lost " and does not turn the call. I need within the add function I can call the upload function, to join views. If I call her by $ this-> upload (); Or simply make all the checking in of add, does not find the component, returning me an error (which I put down ), I believe the conflict is in the request-> data but do not know if there is a way to join 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(__('send'));
                return $this->redirect(['action' => 'listar']);
            }

            $this->Flash->error(__('not send'));
        }


            $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']; //line 32
        $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
        //this is my view add ;
        echo $this->Form->input('id' );
        echo $this->Form->input('titulo');
        echo $this->Form->input('ip');
        echo $this->Form->input('mensagem');

    ?>
       //and this is my view upload, I would like to join with 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();

       ?> 
1
You have a dot after uploadfile remove that and try again. $this->Form->file('uploadfile', ['multiple']); - user3082321
I had already tried it - Lucas L

1 Answers

0
votes

You aren't accessing uploadfile array. This line $this->Upload->send($this->request->data(['uploadfile'])); should be like $this->Upload->send($this->request->data('uploadfile'));.

public function upload()

{

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

}