简体   繁体   English

CakePHP 2.0文件上传不起作用

[英]CakePHP 2.0 file upload not working

Hi Im trying to create a file upload system where I am able to upload a file and it will change the filename to the MYSQL ID that has been uploaded onto the table. 嗨,我正在尝试创建一个文件上传系统,我可以在其中上传文件,它将文件名更改为已上传到表中的MYSQL ID。 This is my code... 这是我的代码...

function add() {
    if (!empty($this->data)) {
        $this->Upload->create();
        if ($this->uploadFile() && $this->Upload->save($this->data)) {
            $this->Session->setFlash(__('<p class="uploadflash">The upload has been saved</p>', true));
            $this->redirect(array('action' => 'add'));
        } else {
            $this->Session->setFlash(__('<p class="uploadflash">The upload could not be saved. Please, try again.</p>', true));
        }
    }
        }

function uploadFile() {
    $file = $this->request->data['Upload']['file'];
    if ($file['error'] === UPLOAD_ERR_OK) {
        if (move_uploaded_file($file['tmp_name'], APP.'webroot/files/uploads'.DS.$this->Upload->id.'.mp4')) {
            $this->Upload->save($this->data);
            return true;
        }
    }
    return false;
}

However the file is not being uploaded to the directory but the information is being uploaded to the sql tabe. 但是,文件未上载到目录,但是信息正在上载到sql tabe。

I don't see why this function $this->Upload->id is not working for the file rename? 我看不到为什么此功能$ this-> Upload-> id对文件重命名不起作用? If I put it in speech marks then it renames the file to "$this->Upload->id.mp4" but I want it to be more like 114.mp4 if thats the field where the information has being saved. 如果我将其放在语音标记中,则它将文件重命名为“ $ this-> Upload-> id.mp4”,但如果那是保存信息的字段,则我希望它更类似于114.mp4。 Anybody have any ideas? 有人有什么想法吗?

Thanks in advance 提前致谢

You need to save first. 您需要先保存。 $this->Upload->id is empty in your case. $ this-> Upload-> id在您的情况下为空。

For your interest: $this->Upload->create(); 为了您的利益:$ this-> Upload-> create(); clears $this->Upload->id 清除$ this-> Upload-> id

See also: 也可以看看:

http://api20.cakephp.org/class/model#method-Modelcreate http://api20.cakephp.org/view_source/model#line-1381 http://api20.cakephp.org/class/model#method-Modelcreate http://api20.cakephp.org/view_source/model#line-1381

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM