Page 1 of 1
unlink() not working, on Windows
Posted: Fri Apr 29, 2005 1:23 pm
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.

Posted: Fri Apr 29, 2005 3:18 pm
by hawleyjr
Is your chmod() working? Check out umask(0); That may help.
Posted: Fri Apr 29, 2005 4:09 pm
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/) їfunction.unlink]: Permission denied in D:\path\writer_details.php on line 97
Those numbers don't mean anything to me.
Posted: Fri Apr 29, 2005 4:22 pm
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 ?
Posted: Fri Apr 29, 2005 4:26 pm
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.
Posted: Fri Apr 29, 2005 8:57 pm
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
Posted: Fri Apr 29, 2005 11:06 pm
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.