CakePHP文件上传到000webhost

时间:2013-09-03 00:12:54

标签: php cakephp file-upload shared-hosting

我想让我的应用程序能够处理上传。这是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 
  

无法上传文件。请再试一次。   一:5:{S:4: “姓名”; S:7: “012.PDF”; S:4: “类型”; S:15: “应用/ PDF”; S:8: “tmp_name的值”; S :14: “/ TMP / phpw0uVGS”; S:5: “错误”; I:0; S:4: “大小”; I:70118;}

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

1 个答案:

答案 0 :(得分:1)

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

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