0

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

3
Contributors
2
Replies
11
Views
7 Years
Discussion Span
Last Post by kumiyare
0

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
}
?>
1

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";
}
?>

Edited by kumiyare: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.