Deleting a file from a directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rasta
Forum Newbie
Posts: 11
Joined: Thu Aug 15, 2002 3:50 pm

Deleting a file from a directory

Post 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
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

try

Post by AVATAr »

lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post 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
Rasta
Forum Newbie
Posts: 11
Joined: Thu Aug 15, 2002 3:50 pm

Post 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
Post Reply