deleteFileFromServer();

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
modplod
Forum Commoner
Posts: 45
Joined: Mon Feb 27, 2006 11:18 am

deleteFileFromServer();

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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';
}
modplod
Forum Commoner
Posts: 45
Joined: Mon Feb 27, 2006 11:18 am

Post by modplod »

cool thanks, I'll give that a try later
:) and let you know the results,
modplod
Forum Commoner
Posts: 45
Joined: Mon Feb 27, 2006 11:18 am

Post 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.";
			}
		}
	}
Post Reply