How to delete a file

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
rangasys
Forum Newbie
Posts: 4
Joined: Wed Nov 23, 2005 4:03 am
Contact:

How to delete a file

Post by rangasys »

I created a back end for a site using PHP..and i have file upload facility in dat

also i need to physically delete the files when user request

so pls tell me the code(functions) to do that
User avatar
phpdevuk
Forum Contributor
Posts: 220
Joined: Mon Jul 04, 2005 5:31 am
Location: UK
Contact:

Post by phpdevuk »

take a look at the unlink function on http://www.php.net
rangasys
Forum Newbie
Posts: 4
Joined: Wed Nov 23, 2005 4:03 am
Contact:

sample

Post by rangasys »

do u have sample code for that??
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Re: sample

Post by JayBird »

rangasys wrote:do u have sample code for that??

Code: Select all

unlink("path/to/the/file.ext");
User avatar
phpdevuk
Forum Contributor
Posts: 220
Joined: Mon Jul 04, 2005 5:31 am
Location: UK
Contact:

Post by phpdevuk »

there is example code on http://www.php.net for it as with most php functions, pimps code is fine
I was assuming you have the filename stored in a mysql database or similar, in which case you can just query the db loop over the results you want to delete, check they exists and delete them, not much to it really.

Code: Select all

// better get filenames from db

$this->sql = "SELECT * FROM filesWHERE fileId='".$fileId."'";
		
$this->result = mysql_query($this->sql);	
		
$row = mysql_fetch_assoc($this->result);
		
// check for files
		
if(is_file("/files/".$row['filename'])){
			
	// delete file
			
	unlink("/files/".$row['filename']);

}
Post Reply