Warning: unlink Permission denied

I use the following in a function in order to delete a certain image from 3 folders:

$post_id	= 	filter_input(INPUT_POST, 'post_id', FILTER_SANITIZE_NUMBER_INT);
$thumb_path = 	APP_PATH.'/../public_html/images/blog_photos/thumbnails/';
$mobil_path = 	APP_PATH.'/../public_html/images/blog_photos/mobile/';
$deskt_path = 	APP_PATH.'/../public_html/images/blog_photos/desktop/';
$photo 		= 	$this->page->get_blog_post_photo($post_id);			
$thumb_file	=	$thumb_path . $photo['post_photo'];		
$mobil_file	=	$mobil_path . $photo['post_photo'];
$deskt_file	=	$deskt_path . $photo['post_photo'];
if (file_exists($thumb_file))
{
	unlink($thumb_file);
}
if (file_exists($mobil_file))
{
	unlink($mobil_file);
}
if (file_exists($deskt_file))
{
	unlink($deskt_file);
}

When I submit thee form I get the following warnings


Warning: unlink(C:\wamp64\www\Flyingparadise\private/../public_html/images/blog_photos/thumbnails/): Permission denied in C:\wamp64\www\Flyingparadise\private\includes\classes\Controller\Admin\Page.php on line 362

Warning: unlink(C:\wamp64\www\Flyingparadise\private/../public_html/images/blog_photos/mobile/): Permission denied in C:\wamp64\www\Flyingparadise\private\includes\classes\Controller\Admin\Page.php on line 366

Warning: unlink(C:\wamp64\www\Flyingparadise\private/../public_html/images/blog_photos/desktop/): Permission denied in C:\wamp64\www\Flyingparadise\private\includes\classes\Controller\Admin\Page.php on line 370

Funny thing is thoug that when i go back using the browser back button thhat the file is indete deleted. It can’t be the Permissions either because is it not only the file I just uploaded but the permissions are set to 777 as well. So actually the function is executed only I get these warning. What could be the reason?

The folder itself has to be executable by everyone. The file doesn’t really matter at this point because it’s not complaining about a specific file. Rather, it’s complaining about 3 folders. There’s a huge difference. Folders hold the hierarchy of permissions. If one folder’s permission is to deny everyone except the owner. Then no matter what even if a certain file has full permission, it’ll get rejected because the folder holds the highest permissions. This is why you need to chmod on the parent folder instead of the file.

@spaceshiptrooper. Thanks for the reply, I didn’t mean that the permissions were set for a file or just one folder. I am testing this locally. Even the main images folder is set to 777

I’m guessing this is on Windows from the C:/ location. Windows has a really weird file system. They don’t follow the *Nix file system so you have to take this in a different approach. I don’t use Windows anymore to develop so my memory is really bad on it. The only thing I can tell you is you may want to try to use chmod(DIRECTORY_HERE, 0777); and see if that works.

Yes it’s on Windows. I am a bit confused myself about this part:

\private/

since public_html and private are on the same level, but I am using the same APP_PATH structure for all other pages as well and there I don’t have any problems at all

Like I said. It won’t matter if they’re on the same level. If that particular folder doesn’t have permissions, then you won’t be able to do anything in there. Right click on the folder and check the permissions.

Seems to me that $photo['post_photo'] is empty so the script will try to remove the entire directories, which is not allowed since you cannot remove non-empty directories in PHP.

@rpkamp. Thanks for the reply. That is the strange thing off it all. To see what is happening i followed the procedure to first upload a new blogpost. No errors there. I get the success message and the post is indeed added to the database and the photo is present in all three folders. After doing that i followed the procedure to delete the just inserted blogpost. And that is the point where I get those warnings. In the meanwhile the post is deleted from the database and the photo is removed from all three folders. I have really no idea why this is happening. For your information. This is the complete function. Maybe you see something out of the ordinary.

	public function drop_postAction()
	{
		if ($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$thumb_path = 	APP_PATH.'/../public_html/images/blog_photos/thumbnails/';
			$mobil_path = 	APP_PATH.'/../public_html/images/blog_photos/mobile/';
			$deskt_path = 	APP_PATH.'/../public_html/images/blog_photos/desktop/';				
			$post_id	= 	filter_input(INPUT_POST, 'post_id', FILTER_SANITIZE_NUMBER_INT);
			$title  	= 	filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING);
			
			// Unlink photos
			$photo 			= 	$this->page->get_blog_post_photo($post_id);
			$thumb_file		=	$thumb_path . $photo['post_photo'];
			$mobil_file		=	$mobil_path . $photo['post_photo'];
			$deskt_file		=	$deskt_path . $photo['post_photo'];
			
			if (file_exists($thumb_file))
			{
				unlink($thumb_file);
			}
			if (file_exists($mobil_file))
			{
				unlink($mobil_file);
			}
			if (file_exists($deskt_file))
			{
				unlink($deskt_file);
			}				
			if ($this->page->delete_blog_post($post_id))
			{
				http_response_code(200);
				echo "Blogpost " . $title . " was successfully deleted from the database. Click the ok button to continue.";
			}
			else
			{
				http_response_code(500);
            	echo "Something went wrong and the post " . $title . " was not deleted.";	
			}
		}
		else
		{
        	http_response_code(403);
        	echo "There was a problem and the post " . $title . "  was not deleted. Please try again.";	
		}
	}

Thank you in advance

Just in adition to the above and maybe important to mention: I had an auto Windows update just 2 days ago and I just noticed that on other sections, where I use a delete function as well, I suddenly get the same messages while a couple of days ago they were still working

Then it might be a php bug. Which php version are you running? Check against bugs for that.

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