Using 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
tstimple
Forum Commoner
Posts: 53
Joined: Wed Jan 21, 2004 10:12 pm

Using UNLINK

Post by tstimple »

Hello,
Thanks to all the professionals who help newbies like me!

I am trying to create a PHP page that will read a mysql table and then DELETE appropriate files on my (hosted) web server.

I have read the documentation about UNLINK, but I am confused about the method to authenticate the user to have permissions to delete files.
I found this code on the PHP documentation website:

Code: Select all

<?php
// removes a file from the hard drive that
// the PHP user has access to.
$username = $_SERVER['REMOTE_USER']; // using an authentication mechanisim

$homedir = "/home/$username";

$file_to_delete = basename("$userfile"); // strip paths
unlink ($homedir/$file_to_delete);

$fp = fopen("/home/logging/filedelete.log","+a"); //log the deletion
$logstring = "$username $homedir $file_to_delete";
fputs ($fp, $logstring);
fclose($fp);

echo "$file_to_delete has been deleted!";
?>
But, I don't understand the line

Code: Select all

<?php
$username = $_SERVER['REMOTE_USER']; // using an authentication mechanisim

?>
Can I pass a username from (for example) a simple form? Or can I simply hardcode a username into my page (and store the page on a secured directory)?
Also, When I tried to implement the code above (possibly incorrectly), I received a "division by zero" error from the line

Code: Select all

<?php
unlink ($homedir/$file_to_delete);
?>
Is there somthing wrong there?

Anyways, I hope someone can shed some light on this for me, or point me to some other examples of how to code using the unlink command.

Thanks,
--TIM
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Not sure about the $_SERVER['REMOTE_USER'], I couldn't really find any information on it. The reason unlink is not working is because the / isn't quoted. It should be:

Code: Select all

<?php
unlink ($homedir . "/" . $file_to_delete);
?>
tstimple
Forum Commoner
Posts: 53
Joined: Wed Jan 21, 2004 10:12 pm

Post by tstimple »

Thanks DuFF,

Can you tell me,

If I comment out the $_SERVER['REMOTE_USER'];
part of the code, And try to simply hardcode in a file name for deletion (for testing purposes) I, of course, get an error "unlink() failed (Permission denied)"

How do I grant permission to the page to do the unlink?

--Tim
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Make sure that the folder you are deleting the files from is CHMOD'ed to 777.

To do this in an FTP program, usually you just have to right-click and go to properties (or options, whatever).
tstimple
Forum Commoner
Posts: 53
Joined: Wed Jan 21, 2004 10:12 pm

Post by tstimple »

Thanks Again for that.

Works fine now!
Post Reply