I've set up a content management system for a client that allows them to change the text on their site, as well as upload images to the server. The MySQL database then drops in the appropriate names in the appropriate rows to refer to the images.
My question is how do I delete the images off of the server should they want to delete an entry. I've got the info being deleted from the DB, but the images remain on the server whether they're being referenced to or not.
I've searched all over the forums, and the tutorials sections, and have found tutorials on uploading and downloading files from a server, but none on deleting files from a server.
Any help would be greatly appreciated.
Thanks
Deleting a file from a directory
Moderator: General Moderators
Rasta,
For the little bit I know. this is how I done it.
hope this helps a bit.
Jah-Rasta-Fariiiiiiiiii
For the little bit I know. this is how I done it.
Code: Select all
<?php
//to strip away the file name from the database showing the full URL
$name = substr(strrchr ($tag, "/"), 1);
// for me the following line was a varible based on customer
$folder = "$upload";
// the following line will be different for your server
define ("PATH", "/home/httpd/vhosts/net.uk.com/httpdocs/support/docs/user/",TRUE);
?>
<?php
include('connect.php');
$connection = @mysql_connect($host, $user, $pass) or die ("Unable to connect to database");
mysql_select_db($db) or die ("Unable to select database: $db ");
unlink(PATH.$folder."/".$name)
or die('Could Not delete File');
//some database stuff you probably wont need.
$sql="UPDATE projdocs SET deleted = 'Yes' WHERE doc_id = '$doc_id' " ;
mysql_query($sql) or die('An error occured executing sql statement. SQL='.$sql.'<br>'.mysql_error());
echo "$name has been deleted successfully.";
?>Jah-Rasta-Fariiiiiiiiii