Member Avatar for doctorphp

Hi All.

I am currently designing a photo sharing website. I need to make a script that deletes the image from the folder it is stored in and also from the database. I have done the database bit but i cannot work out the deleting from the folder bit.

Any help would be much appreciated.
Thanks in advance,
Cameron

Recommended Answers

All 2 Replies

To delete a file (i.e. image in a folder), use the PHP function unlink:

<?php
if( unlink('/path/to/file/imagenn.jpg') ) {
 // File delete worked
}
else {
 // File delete failed
}
?>

To delete the entry from the database and remove the associated photo or file use the following code

<?php
// The code assumes that you are deleting multiple entries at once using check-box, so it is looping.. 
	$qry=mysql_query("select * from TABLE where id='$id'");
	$row=mysql_fetch_array($qry);

	if($row["photo"]!="") {  
	$photo=$row["photo"];
	unlink("/path/to/photo/".$photo);
	}
	if($row["file"]!="") {  
	$file=$row["file"];
	unlink("/path/to/file/".$file);
	} 	
// Delete the entry from the database
$delete="Delete from TABLE where id='$id' ";
$result=mysql_query($delete);
if($result==1){
print "Entry delete success";
}else{
print "Entry delete failed";
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.