Page 1 of 1

How to delete a file

Posted: Wed Nov 23, 2005 4:18 am
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

Posted: Wed Nov 23, 2005 4:18 am
by phpdevuk
take a look at the unlink function on http://www.php.net

sample

Posted: Wed Nov 23, 2005 4:32 am
by rangasys
do u have sample code for that??

Re: sample

Posted: Wed Nov 23, 2005 4:39 am
by JayBird
rangasys wrote:do u have sample code for that??

Code: Select all

unlink("path/to/the/file.ext");

Posted: Wed Nov 23, 2005 4:53 am
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']);

}