unlink

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

unlink

Post by shiznatix »

when i try to unlink a directory on a windows machine i get permission denied. this is (i think) becuase of whatever the files '.' and '..' are becuae when i try to delete everything it in first then delete the dir but i get permission denied for those files then permission denied for the folder. how do i get around this?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

same exact problem. i get permission denied for those . and .. files when trying to delete them. if i try to just delete the whole dir without the files first i get permission denied dir not empty.

Code: Select all

Warning: unlink(../gallerys/1/..) [function.unlink]: Permission denied
Warning: unlink(../gallerys/1/.) [function.unlink]: Permission denied
Warning: rmdir(../gallerys/1) [function.rmdir]: Permission denied
how can i get rid of those pesky stupid things?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Ensure you have the correct permissions, and that the files are not in use.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

its a windows computer, there are no permissons. the folder is not in use as it can be deleted by just going to my hard drive and hitting delete. everything else in the folder gets deleted no problem so i don't see why these files that are not really there are stopping me from deleting the folder.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Use the commands like this:

Code: Select all

unlink(realpath('../images/image.jpg'));
Windows doesn't recognise the whole ../. thing. Well, it does, but it's fussy.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

same problem. its still these damned . and .. files that won't delete
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

. and .. are not files, they are shortcuts :) '.' = current directory and '..' = parent directory.

I just tested this myself, also on windows with the following that I just made up:

Code: Select all

function delDir($dir) {
	$dir = realpath($dir);
	if (!is_dir($dir)) {
		return false;
	} else {
		$hand = opendir($dir);
		while (($file = readdir($hand)) !== false) {
			if (($file != '..') && ($file != '.')) {
				unlink(realpath($dir . '/' . $file));
			}
		}
		closedir($hand);
		chmod($dir, 0777);
		if (!rmdir($dir)) {
			return false;
		}
		return true;
	}
}
And it works..

I have read up a bit, and it appears the PHP.ini directive safe_mode causes problems with functions like these, so check that to ensure it is set to off/0. maybe even include the line

Code: Select all

ini_set('safe_mode', 0);
in your script before using rmdir?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

ok that works. the problem was i would open the dir and look at the pictures and it would make a thumbs.db and whatnot. if i dont open the folder on my computer ever it works fine. stupid things. thanks
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Aye, one of the many things listed on annoyances.org.
Post Reply