unlink() not working, on Windows

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
sethpackham
Forum Newbie
Posts: 11
Joined: Fri Nov 07, 2003 3:28 pm
Location: Apex, NC

unlink() not working, on Windows

Post by sethpackham »

I've got a simple script where members can upload a photo. When they upload a new photo, I want to delete the old photo.

Everything works but the unlink(), which says permission denied. I've tried lots of different methods for chmod'ing the old photo and the new photo upon upload, as well as the folder where the photos are stored.

Code: Select all

$myphotofilename = str_replace(" ", "_", $_FILES["writer_photo"]["name"]);
$oldphoto = $myrow["photofilename"];
				
//rename($photos_path . $myrow['photofilename'], $photos_path . $myphotofilename);
//copy the file to some permanent location
if (move_uploaded_file($_FILES["writer_photo"]["tmp_name"], $photos_path .   $myphotofilename)) {
	echo "<p>File uploaded to: " . $photos_path . $myphotofilename . "\n";
	chmod ($photos_path, 0777); // octal; correct value of mode
	chmod ($photos_path . $myphotofilename, 0777); // octal; correct value of mode
	//let's delete the old photo
	chmod ($photos_path . $oldphoto, 0777);
	unlink($photos_path . $oldphoto);
}
I get this warning:
File uploaded to: D:/path/photos/drew1.jpg
Warning: unlink(D:/path/photos/) [function.unlink]: Permission denied in D:\path\writer_details.php on line 89

Any ideas would be greatly appreciated! I'm sure I'm doing something wrong. :?
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Is your chmod() working? Check out umask(0); That may help.
sethpackham
Forum Newbie
Posts: 11
Joined: Fri Nov 07, 2003 3:28 pm
Location: Apex, NC

Post by sethpackham »

How can I tell if my chmod is working? The umask() method that you suggested has confused me further. Everywhere I look, umask is defined in a circular fashion: umask sets the umask (something to do with the default file permissions given to new files and folders?).

Is it possible that all of this only works with Unix flavors, but not with windows file permissions? Has anyone successfully done an unlink() on Windows?

I've now got this:

Code: Select all

if (move_uploaded_file($_FILES["writer_photo"]["tmp_name"], $photos_path . $myphotofilename)) {
echo "<p>File uploaded to: " . $photos_path . $myphotofilename . "\n";
$old = umask(0);
echo "<br>old umask: " . $old;
chmod($photos_path, 0777); // octal; correct value of mode
chmod($photos_path . $myphotofilename, 0777); // octal; correct value of mode

//let's delete the old photo
chmod($photos_path . $oldphoto, 0777);
$perms = fileperms($photos_path . $oldphoto);
echo "<br>old file perms: " . $perms;
$perms = fileperms($photos_path . $myphotofilename);
echo "<br>new file perms: " . $perms;
unlink($photos_path . $oldphoto);
umask($old);
}
Which returns this:

Code: Select all

File uploaded to: D:/path/photos/diane1.jpg
old umask: 384
old file perms: 16895
new file perms: 33206
Warning: unlink(D:/path/photos/) &#1111;function.unlink]: Permission denied in D:\path\writer_details.php on line 97
Those numbers don't mean anything to me.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Code: Select all

#the following is better by the way.
if (is_uploaded_file($_FILES['whatever_file']['tmp_name']))
{
copy($_FILES['whatever_file']['tmp_name'], $photos_path."/".$myphotofilename);
}
Did you check if the file is really there before trying to delete it ?
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

The error is a user permission error. Delete the dir and create it using mkdir() Depending on your server if the dir was created outside of PHP the dir will have a different owner which will disallow you to change the properties of the dir.
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Hi,

You could have the photo renamed to the users id upon upload, so when they change their photo it will just over ride the image on the server automatically. This will also eliminate the blank space problems within their photo filename.

Well thats what I do and it works fine.

Bob
sethpackham
Forum Newbie
Posts: 11
Joined: Fri Nov 07, 2003 3:28 pm
Location: Apex, NC

Post by sethpackham »

Woohoo! I finally got unlink() to work on my Windows server after I discovered a couple of earlier mistakes in my code and tweaked a million other things. In the meantime, I learned more about several PHP methods and some coding techniques. Thanks for your help.

This is what is now working beautifully for me (If you have any code syntax tips, I love getting those, since I feel like I'm always making it up):

Code: Select all

$myphotofilename = str_replace(" ", "_", $_FILES["writer_photo"]["name"]);
$oldphoto = $myrow["writer_photofilename"];

if (is_uploaded_file($_FILES["writer_photo"]["tmp_name"])) {
  copy($_FILES["writer_photo"]["tmp_name"], $photos_path . "/" . $myphotofilename);
  chmod($photos_path . "/" . $myphotofilename, 0666);
  echo "<p>New photo uploaded: " . $myphotofilename . "\n";
  if ((!empty($oldphoto)) and ($oldphoto != $myphotofilename)) {
    if (unlink($photos_path . "/" . $oldphoto)) {
      echo "<p>Old photo deleted: " . $oldphoto;
    }
  }
}
At one point I was sending a bad value for $oldphoto, but that should have just yielded a "file not found" on the unlink(), not a "permission denied", so I think it's hard (for me anyway) to pinpoint exactly what I was doing wrong. Aligned the planets finally, I guess.
Post Reply