我正在尝试从我的webroot / img / uploads文件夹中删除图像文件。但是我已经成功删除了我的mysql数据库表中的条目,但我还需要从webroot文件夹中删除硬拷贝......
以下是我的删除照片的控制器功能
public function delete_photo($id){
$con['Photo.id'] = $id;
$the_photo = $this->Photo->find('first',array('conditions' => $con));
//var_dump($the_photo);
//echo $the_photo['Photo']['image_name'];
$this->Photo->delete($id);
function afterDelete($id)
{
$file = new File(WWW_ROOT .'/img/uploads/'. $the_photo['Photo']['image_name'],false, 0777);
if($file->delete()){
echo "File deleted";
}
else{
echo "Deletion failed!!";
}
}
$this->autoRender = false;
}
答案 0 :(得分:4)
试试这个
您正在使用功能:
try {
$data = $this->Document->find('all',array('conditions'=>array('id' => $id)));
$f = $data[0]['Document']['doc_file'];
$documents = $this->Document->delete($id);
$file = new File(WWW_ROOT .$f,false, 0777);
if($file->delete()){
$this->Session->setFlash('Deleted Successfully', 'default', array('class' => 'succuss'));
$this->redirect('add');
} else {echo 'files not deleted';}}
} catch (Exception $e){
// Do something with an exception
}
答案 1 :(得分:1)
你可以通过php unlink函数简单删除文件。
$data = $this->Document->find('first',array('conditions'=>array('id' => $id))); $f = $data['Document']['doc_file']; @unlink(YOUR_ROOT_PATH.$f);