0

您好我正在尝试创建一个文件上传系统,我可以在其中上传文件,它将文件名更改为已上传到表中的 MYSQL ID。这是我的代码...

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

但是文件没有上传到目录,但信息正在上传到 sql 表。

我不明白为什么这个函数 $this->Upload->id 不适用于文件重命名?如果我把它放在语音标记中,那么它会将文件重命名为“$this->Upload->id.mp4”,但如果那是保存信息的字段,我希望它更像 114.mp4。有人有什么想法吗?

提前致谢

4

1 回答 1

0

您需要先保存。$this->Upload->id 在您的情况下为空。

为了您的兴趣: $this->Upload->create(); 清除 $this->Upload->id

也可以看看:

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

于 2012-03-06T13:24:23.640 回答