fileExistsInPath cakePHP function to check if File/Image Exists in valid Path

Saturday, February 13, 2010 13:01
function deletephotograph($id){
  //Get File/Image name
 $data = $this->Propphotograph->find('first', array('conditions' => array('Propphotograph.id' => $id), 'fields' => array('Propphotograph.photograph')));
  $image_name = $data['Propphotograph']['photograph'];

  // Use fileExistsInPath() function to check valid File/Image Path
  $thumbPath = 'img/props_photographs/thumbs/'.$image_name.'';
  $imagePath = 'img/props_photographs/'.$image_name.'';
  if (fileExistsInPath($imagePath)){unlink($imagePath);} // Delete Image if it exists
  if (fileExistsInPath($thumbPath)){unlink($thumbPath);} // Delete Image Thumb if it exists

  $this->Propphotograph->delete($id); // Delete Record
  $this->Session->setFlash('The photograph has been deleted.');
  $this->redirect($this->referer());
}
  • Share/Bookmark
You can leave a response, or trackback from your own site.

Leave a Reply