在000webhost上传CakePHP文件


CakePHP file upload on 000webhost

我正在努力让我的应用程序能够处理上传。这是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:"error";i:0;s:4:

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

解决方案:不要试图自己编写代码。使用CakePHP现有的文件上传插件之一,它将处理所有幕后的细节,只需最少的代码。

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