Page 1 of 1

Deleting a file from a directory

Posted: Mon Nov 04, 2002 5:14 pm
by Rasta
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

try

Posted: Mon Nov 04, 2002 6:12 pm
by AVATAr

Posted: Mon Nov 04, 2002 6:31 pm
by lloydie-t
Rasta,
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."; 
?>
hope this helps a bit.
Jah-Rasta-Fariiiiiiiiii

Posted: Mon Nov 04, 2002 11:53 pm
by Rasta
And the "unlink" wins it! That was just what I was looking for.

Thanks AVATAr, and lloydie-t. I much appreciate the help. :P