我正在做一些简单的博客网站的实践,我试图显示webroot/img/xxx/abc.(jpg,jpeg,png)内上传的文件.在控制器中,我可以上传文件,并添加在DB中,但不能显示它的视图文件,我不知道为什么.
这是我想显示我上传的文件的地方(view.php)
<td><?= $this->Html->image($post->titleImg_file_name, ["height"=> 50, "width"=>50]) ?></td>
这是我上传文件的控制器。
public function add()
{
$post = $this->Posts->newEmptyEntity();
if ($this->request->is('post')) {
$post = $this->Posts->patchEntity($post, $this->request->getData());
$file = $this->request->getData("titleImg_file_name");
// If no dir exits, make one
$dir = realpath(WWW_ROOT . "img" . DS . date("Ymdhms"));
if(!$dir){
$dir = mkdir(WWW_ROOT . "img" . DS . date("Ymdhms"), 0744);
$realPath = WWW_ROOT . "img" . DS . date("Ymdhms");
move_uploaded_file($file["tmp_name"], $realPath.DS.$file["name"]);
$post->titleImg_file_name = date("Ymdhms"). "/" .date("Ymdhms").$file["name"];
// dd($post);
}
if ($this->Posts->save($post)) {
$this->Flash->success(__('The post has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The post could not be saved. Please, try again.'));
}
$users = $this->Posts->Users->find('list', ['limit' => 200]);
$tags = $this->Posts->Tags->find('list', ['limit' => 200]);
$this->set(compact('post', 'users',"tags"));
}
这是视图控制器。
public function view($id = null)
{
$post = $this->Posts->get($id, [
'contain' => ['Users'],
]);
$this->set(compact('post'));
}
任何帮助我都将感激不尽。
1条答案
按热度按时间mzsu5hc01#
我解决了这个问题。我创造了错误的道路。