我正在将文件上传到CakePHP中的文件夹。我编写了以下代码。文件的名称已正确插入数据库,但文件未上传。
function addtickets(){
$this->data['Ticket']['attachment']=date('YmdHis').$this->data['Ticket']['attachment']['name'];
if ($this->Ticket->save($this->data)){
$target_path = "bug/app/tmp/uploads/";
$target_path = $target_path . basename( $_FILES['Ticket']['attachment']['name']);
if(move_uploaded_file($_FILES['Ticket']['attachment']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['Ticket']['attachment']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
$this->Session->setFlash('Ticket created');
} else {
$this->Session->setFlash('Cannot create a ticket');
}
}
我在位置bug/app/tmp/uploads
上传了文件夹并且它是可写的。
但是点击提交按钮后,所有插入数据库但是文件的值都没有上传。
请帮助 感谢
答案 0 :(得分:2)
试试这个
$new_file_location = APP.'tmp'.DS.'uploads'.DS.$this->data['Ticket']["attachment"]['name'];
move_uploaded_file($this->data['Ticket']["attachment"]['tmp_name'], $new_file_location);