Unlink file from multiple folders

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

Check your error logs. You’ll find an exact problem if you look at your error logs. If nothing is reported to your error logs, then I suggest making sure you have permission to modify or add to those directories.

Hi @spaceshiptrooper. I made an edit to my post, maybe that makes more sense

Edit:
and I just looked but there is no error in the log file

What I suggest is adding in a die or exit with a simple message saying “Here” or something that allows you to recognize it. Then I’d suggest starting at the top of the function and working your way down. If you see the message “Here”, that means that it’s being executed correctly. Once it disappears, that’s when you know there’s something wrong.

Another thing I do is echo the variables as you go to confirm they contain what you expect.

@ Rubble Thats i what I thought, so I did. I took the AJAX request out and placed the action just in the form. In the function I took everything out and made it like this:

	public function manage_header_photo()
	{
		$photo_id          	= filter_input(INPUT_POST, 'photo_id', FILTER_SANITIZE_NUMBER_INT);						
		echo $photo_id;
	}

but then get a fatal error:

**Fatal error:** Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in C:\wamp64\www\Flyingparadise\private\config\routes.php on line 3

Pfffff I houldn’t work any longer when I become tired. Look at the difference between the two functions I mentioned:

public function manage_header_photo()
public function delete_gallery_photosAction()

I forgot the entire Action() part in the one that wasn’t working. Thank you all for the reply and sorry for the inconvenience

iIt is worth noting that the PHP unlink(…) function returns a Boolean success value. This should have been checked and the error pinpointed immediately.

http://php.net/manual/en/function.unlink.php

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.