Page 1 of 1

deleteFileFromServer();

Posted: Tue Mar 14, 2006 12:54 pm
by modplod
hi all, I was wondering what the easest way to enable a user to remove a file from the server would be? and where I may find an example to get me started?

Thanks

Posted: Tue Mar 14, 2006 2:43 pm
by John Cartwright

Code: Select all

function deleteFile($filepath) {
   return (file_exists($filepath) && unlink($filepath));
}
Might want to do some validation on $filepath first though.. make sure they are deleting any files they shouldn't be.

example usage

http://domain.com/delete.php?filepath=somefile.jpg

Code: Select all

if (!empty($_GET['filepath']) && deleteFile($_GET['filepath'])) {
   echo 'file succesfuly deleted';
}

Posted: Wed Mar 15, 2006 11:47 am
by modplod
cool thanks, I'll give that a try later
:) and let you know the results,

Posted: Wed Mar 15, 2006 12:15 pm
by modplod
using what you gave me I threw this togeather, do you think it will work to prevent people from removing files out side of the fileRoot dir?

Code: Select all

function deleteFileFromServer($filepath) {
		if ($filepath){
			// check that the file is within public root folder and not a system file
			if(stristr($filepath, $this->fileRoot) === FALSE) {
				echo "You do not have permission to remove that file from the server!";
				return false;
			}elseif (stristr($filepath, $this->fileRoot) === TRUE) {
				return (file_exists($filepath) && unlink($filepath));
			}else{
				echo "Invalid File, Please select another file.";
			}
		}
	}