I try to use the following function to unlink a file from multiple folders and have it deleted from the database:
public function manage_header_photo()
{
$photo_id = filter_input(INPUT_POST, 'photo_id', FILTER_SANITIZE_NUMBER_INT); // hidden field in form
$thum_path = APP_PATH.'/../public_html/images/masterhead_photos/thumbnails/';
$mobi_path = APP_PATH.'/../public_html/images/masterhead_photos/mobile/';
$desk_path = APP_PATH.'/../public_html/images/masterhead_photos/desktop/';
$photo = $this->media->get_masterhead_id($photo_id);
$thum_file = $thum_path . $photo['photo'];
$mobi_file = $mobi_path . $photo['photo'];
$desk_file = $desk_path . $photo['photo'];
if (file_exists($thum_file))
{
unlink($thum_file);
}
if (file_exists($mobi_file))
{
unlink($mobi_file);
}
if (file_exists($desk_file))
{
unlink($desk_file);
}
$this->media->delete_masterhead_photo($photo_id );
}
I submit the form with AJAX and get a success message but nothing is happening. Nothing is unlinked and nothing is deleted from te DB. Does anyone see what is wrong in my function or in the other case how I can do this better?
Thank you in advance
Edit: I use the exact same function in another page in the CMS for the same site with the only difference that on that page , multiple image can be deleted so I use a loop over the id’s
public function delete_gallery_photosAction()
{
$thumb_path = APP_PATH.'/../public_html/images/photos/thumbnails/';
$photo_path = APP_PATH.'/../public_html/images/photos/photos/';
$photos = $this->media->get_photo_ids($_POST['delete_id']);
foreach ($photos as $photo)
{
$thumb_file = $thumb_path . $photo['photo'];
$photo_file = $photo_path . $photo['photo'];
if (file_exists($thumb_file))
{
unlink($thumb_file);
}
if (file_exists($photo_file))
{
unlink($photo_file);
}
}
$this->media->delete_gallery_photos($_POST['delete_id']);
}
and there every works fine