CakePHP 3.x调用函数

时间:2015-12-15 19:20:36

标签: cakephp cakephp-3.0

我有一个视图,即添加一个名为帮助台的开放表单(当然是指添加功能),并且此功能是将该文件上传到附加请求,该请求与组件连接,但我不想让他们在不同的视图中,因为如果我只是通过视图发送文件上传,那么图像就会丢失"并且不转接电话。我需要在add函数中调用upload函数来加入视图。如果我用$ this->给她打电话upload();或者只是进行添加的所有检查,找不到组件,给我一个错误(我放下),我相信冲突在请求中 - >数据,但不知道是否有办法加入我解释的方式。

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']);
    }

}

组件:

      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);
        }
    }
}

}

调用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]

视图:

  <?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 个答案:

答案 0 :(得分:0)

您无法访问uploadfile array。这一行$this->Upload->send($this->request->data(['uploadfile']));应该与$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']);
    }

}