Cakephp 在上载概念中,无法将文件名上载到数据库
在我的UsersController.php中Cakephp 在上载概念中,无法将文件名上载到数据库,cakephp,Cakephp,在我的UsersController.php中 public function uploadFile() { $filename = ''; if ($this->request->is('post')) { $uploadData = $this->data['uploadFile']['image']; if ( $uploadData['size'] == 0 || $uploadData['error
public function uploadFile()
{
$filename = '';
if ($this->request->is('post')) {
$uploadData = $this->data['uploadFile']['image'];
if ( $uploadData['size'] == 0 || $uploadData['error'] !== 0) {
return false;
}
$filename = basename($uploadData['name']);
if ($this->User->save($filename)) {
$this->Session->setFlash(__('Your imagename has been saved.'));
}
$uploadFolder = WWW_ROOT. 'files';
$filename = time() .'_'. $filename;
$uploadPath = $uploadFolder . DS . $filename;
if( !file_exists($uploadFolder) ){
mkdir($uploadFolder);
}
if (!move_uploaded_file($uploadData['tmp_name'], $uploadPath)) {
//$this->set('pdf_path', $fileName);
return false;
}
}
}
}
在我的uploadfile.ctp中
<?php
echo $this->Form->create('uploadFile', array( 'type' => 'file'));
echo $this->Form->input('image', array('type' => 'file','label' => 'Pdf'));
echo $this->Form->end('Upload file');
?>
在我的数据库中,我有表名“image”以及一些其他字段。现在我无法将文件名上载到数据库。如果您有一个名为“image”的表名,那么它应该与用户模型相关$此->用户->保存($filename)在用户表中至少需要一个名为“filename”的表字段,并且需要通过$this->User->id=$User\u id将其分配给用户
如果您的图像表具有与用户模型相关的图像模型,请阅读本书的“保存相关模型数据”一章。我们可以通过执行$data=array('user'=>array('file\U name'=>$filename,)来保存文件名;$this->User->save($data);是的,你可以。但再次强调:它将在用户表中创建一条新记录(如果在用户模型中没有字段列为“必需”),而不是更新,因为您在保存查询中没有发送相关的用户id。您希望每个用户上载多少图像?