unlink() permission denied?

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
dimovi
Forum Newbie
Posts: 7
Joined: Wed Nov 22, 2006 11:29 pm
Location: Austin, TX, USA
Contact:

unlink() permission denied?

Post by dimovi »

Code: Select all

/*
I have Apache 2.0 and PHP 5.1.4 on WinXP Pro
When I try to delete a file with unlink() it works fine if the directory does not contain characters such as '~', '+' etc.
Any idea why that is?
Does anyone know how to take care of that?

ex.
*/
unlink("Austin/2003-05-29 P + UT/PICT0011.JPG");   // would not work
unlink("Austin/2003-05-29 P UT/PICT0011.JPG");   // works
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Try

Code: Select all

unlink("Austin/2003-05-29\ P\ +\ UT/PICT0011.JPG");
dimovi
Forum Newbie
Posts: 7
Joined: Wed Nov 22, 2006 11:29 pm
Location: Austin, TX, USA
Contact:

It didn't work

Post by dimovi »

Code: Select all

unlink("Austin/2003-05-29\ P\ +\ UT/PICT0011.JPG"); //does not work
// and neither does
unlink("Austin/2003-05-29 P \+ UT/PICT0011.JPG");

// the problem could not be in the spaces because it works fine when there is no '+'
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Win xp pro, php 5.2.0, and I can't reproduce the error.
unlink("Austin/2003-05-29 P + UT/PICT0011.JPG");
file is gone.
dimovi
Forum Newbie
Posts: 7
Joined: Wed Nov 22, 2006 11:29 pm
Location: Austin, TX, USA
Contact:

Post by dimovi »

I don't know what to do man.
It seems like nobody has had that problem.
It is probably something small, but...
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Yes, I've spent so many time on deleting the folder with space. It worked for folder without space but not with spaces.
Note : I'm working on linux machine.

I've tried the following with so many way but ended in failure :oops:

Code: Select all

<?php

//shell_exec("mkdir 2003-05-29\ P\ UT");
$dir = "2003-05-29\\ P\\ +\\ UT";

$file = "PICT0011.jpg";

$path_to_file = $dir."/".$file;

if(!file_exists($dir)) { shell_exec("mkdir $dir");}

if(!file_exists($path_to_file)) { shell_exec("touch $path_to_file"); }

if(file_exists($path_to_file))
print $path_to_file;

shell_exec("chmod 755 $dir");
shell_exec("chmod 755 $path_to_file");

print shell_exec("ls -lth $dir/");

if(shell_exec("rm -rf $path_to_file")){
	print "deleted ". $path_to_file;
}else{
	print "error occured!";
}
?>
Output :

Code: Select all

total 0
-rwxr-xr-x 1 root root 0 2006-11-24 17:33 PICT0011.jpg
error occured!
But the following works if the folder is given without space like :

Code: Select all

<?php

//shell_exec("mkdir 2003-05-29\ P\ UT");
$dir = "2003-05-29";
//$dir = addslashes($dir);

$file = "PICT0011.jpg";

$path_to_file = $dir."/".$file;

//if(!file_exists($dir)) { mkdir($dir); }
if(!file_exists($dir)) { shell_exec("mkdir $dir");}

if(!file_exists($path_to_file)) { shell_exec("touch $path_to_file"); }

if(file_exists($path_to_file))
print $path_to_file;

//chmod($dir, 0777);
//chmod($path_to_file, 0777);
shell_exec("chmod 755 $dir");
shell_exec("chmod 755 $path_to_file");

print shell_exec("ls -lth $dir/");

if(unlink("$path_to_file")){
	print "deleted ". $path_to_file;
}else{
	print "error occured!";
}

?>
Output :

Code: Select all

2003-05-29/PICT0011.jpgtotal 0
-rwxr-xr-x 1 root root 0 2006-11-24 17:36 PICT0011.jpg
deleted 2003-05-29/PICT0011.jpg
Dibyendra
Post Reply