954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Delete From Folder and Database

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

doctorphp
Junior Poster in Training
67 posts since Apr 2010
Reputation Points: 10
Solved Threads: 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
}
?>
Devoted Hosting
Light Poster
30 posts since Nov 2009
Reputation Points: 12
Solved Threads: 6
 

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";
}
?>
kumiyare
Newbie Poster
8 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: