我有一个表单,我试图上传图像并尝试将其存储在数据库中.我的控制器代码是:
class OlxProductsController extends AppController{
public function post(){
if($this->request->is('post')) {
if ($this->request->data['OlxProduct']['img_upload']) {
$file = $this->data['OlxProduct']['img_upload']; //put the data into a var for easy use
// print_r($file);
$filename = str_replace(" ", "-", rand(1, 3000) . $file['name']);
move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/uploads/' . $filename);
//echo $filename; die();
$this->request->data['OlxProduct']['img_upload'] = $filename;
} else {
unset($this->request->data['OlxProduct']['img_upload']);
}
$this->OlxProduct->save($this->request->data);
$this->Session->setFlash('posted successfully');
$this->redirect(array('controller' => 'OlxUsers', 'action' => 'account'));
}
else {
$this->Session->setFlash('ad has not been saved');
}
}
}
Run Code Online (Sandbox Code Playgroud)
邮政行动代码是://post.ctp
<form action="../post" method="post" enctype="multipart/form-data" >
<?php …Run Code Online (Sandbox Code Playgroud)