0

我试图让我的应用程序能够处理上传。这是 DealsController.php 中的添加操作。

<?php
        public function add() {
        if ($this->request->is('post')) {
            $this->request->data['Deal']['user_id'] = $this->Auth->user('id');
            $this->Deal->create();
            if ($this->Deal->uploadFile($this->request->data['Deal']['pic'])){
                $file = $this->request->data['Deal']['pic'];
                $this->request->data['Deal']['pic'] = $this->request->data['Deal']['pic']['name'];
                if ($this->Deal->save($this->request->data)) {
                    $this->Session->setFlash(__('Your deal has been saved. %s', h(serialize($file)) ));
                    return $this->redirect(array('action' => 'index'));
                }
                $this->Session->setFlash(__('Unable to add your deal. %s', h(serialize($file)) ));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('Unable to upload the file. Please try again. %s', h(serialize($this->request->data['Deal']['pic']))));
        }
    }

uploadFile函数在 Deal.php 中的模型中定义

<?php
public function uploadFile( $check ) {

    $uploadData = array_shift($check);

    if ( $uploadData['size'] == 0 || $uploadData['error'] !== 0) {
        return false;
    }

    $uploadFolder = 'files'. DS .'your_directory';
    $fileName = time() . '.pdf';
    $uploadPath =  $uploadFolder . DS . $fileName;

    if( !file_exists($uploadFolder) ){
        mkdir($uploadFolder);
    }

    if (move_uploaded_file($uploadData['tmp_name'], $uploadPath)) {
        $this->set('pic', $fileName);
        return true;
    }

    return false;
}

`uploadFile` keeps returning false and I get my 

无法上传文件。请再试一次。a:5:{s:4:"name";s:7:"012.PDF";s:4:"type";s:15:"application/pdf";s:8:"tmp_name";s :14:"/tmp/phpw0uVGS";s:5:"错误";i:0;s:4:"大小";i:70118;}

闪光。这令人困惑,因为它看起来像是文件实际上已上传到临时目录。我尝试了不同的路径,move_uploaded_file但没有任何效果。这与文件权限有关吗?它不可能是文件大小,因为我的上传文件只有大约 80 KB。

4

1 回答 1

1

解决方案:不要尝试自己编写代码。使用现有的 CakePHP 文件上传插件之一,它将处理幕后的所有来龙去脉,而您的编码最少。

我个人使用https://github.com/josegonzalez/upload

于 2013-09-03T02:53:15.813 回答