move_uploaded_file 保存在错误的目的地
move_uploaded_file saves on wrong destination
我正在尝试让这段代码正常工作:
if ($this->request->is(['patch', 'post', 'put'])){
$nameImg = $this->request->getData()['imagem']['name'];
$imgTmp = $this->request->getData()['imagem']['tmp_name'];
$destination = "files\user\".$user_id."\".$nameImg;
if(move_uploaded_file($imgTmp,WWW_ROOT . $destination)){
$this->Flash->success(__('Success!'));
}
}
但每次我尝试 运行 时,图像都保存在我的 webroot 目录中,而不是我指定的 $destination 路径中,所有目录都设置为 777 和 move_uploaded_file returns 是的,我该怎么做才能让它保存在指定的路径上?
这是$this->request->getData();
调试输出:
[
'imagem' => [
'tmp_name' => '/tmp/phpTrcN6K',
'error' => (int) 0,
'name' => 'icone1.png',
'type' => 'image/png',
'size' => (int) 15778
]
]
尝试在您的路径中使用目录分隔符 (DS):
if ($this->request->is(['patch', 'post', 'put'])){
$nameImg = $this->request->getData('imagem.name');
$imgTmp = $this->request->getData('imagem.tmp_name');
$destination = WWW_ROOT . 'files' . DS . 'user' . DS . $user_id . DS . $nameImg;
if(move_uploaded_file($imgTmp, $destination)){
$this->Flash->success(__('Success!'));
}
}
我正在尝试让这段代码正常工作:
if ($this->request->is(['patch', 'post', 'put'])){
$nameImg = $this->request->getData()['imagem']['name'];
$imgTmp = $this->request->getData()['imagem']['tmp_name'];
$destination = "files\user\".$user_id."\".$nameImg;
if(move_uploaded_file($imgTmp,WWW_ROOT . $destination)){
$this->Flash->success(__('Success!'));
}
}
但每次我尝试 运行 时,图像都保存在我的 webroot 目录中,而不是我指定的 $destination 路径中,所有目录都设置为 777 和 move_uploaded_file returns 是的,我该怎么做才能让它保存在指定的路径上?
这是$this->request->getData();
调试输出:
[
'imagem' => [
'tmp_name' => '/tmp/phpTrcN6K',
'error' => (int) 0,
'name' => 'icone1.png',
'type' => 'image/png',
'size' => (int) 15778
]
]
尝试在您的路径中使用目录分隔符 (DS):
if ($this->request->is(['patch', 'post', 'put'])){
$nameImg = $this->request->getData('imagem.name');
$imgTmp = $this->request->getData('imagem.tmp_name');
$destination = WWW_ROOT . 'files' . DS . 'user' . DS . $user_id . DS . $nameImg;
if(move_uploaded_file($imgTmp, $destination)){
$this->Flash->success(__('Success!'));
}
}