PDA

View Full Version : Php -- Deleting Images



Morgenmuffel
14-08-2005, 05:37 PM
Hi all

Ok this my 7th rewrite, so hopefully it's coherent-ish

I have a database in the following form

product table

ID
Name
Price
Size
Image --- (link to image eg /images/bob.gif)

Now if i want to delete a product I can do that easily enough through mysql

But

This only deletes the link to the image, not the actual image, I want to delete both obviously

I have searched Google and yahoo, but all these seem to show is how to selete a record, (or in some case people seem to store images in the dbase)

Perhaps i am searching incorrectly, I have seen mention of an "unlink()" function but am unsure wether this actually removes the file or just severs connection to the file for hat session -- (sorry but that is the bit I am hazy on)


As always any help would be much appreciated

sal
14-08-2005, 08:51 PM
Hi all
Now if i want to delete a product I can do that easily enough through mysql

But

This only deletes the link to the image, not the actual image, I want to delete both obviously...Perhaps i am searching incorrectly, I have seen mention of an "unlink()" function but am unsure wether this actually removes the file...
nope, thats what your looking for

http://nz.php.net/unlink

bartsdadhomer
14-08-2005, 09:00 PM
Here's a snippet from one of my sites
Works well

<?
require 'config/config.php';
require 'secure.php';

// remove image from listing
if($id) {
// get filenames from database
$link = mysql_connect($dbhost, $dbuser, $dbpass);
$query = "SELECT image, thumb FROM $dbimg WHERE id='$id'";
$result = mysql_db_query($dbname, $query, $link);
$data = mysql_fetch_array($result);

// delete image and thumbnail
unlink("thumbs/$data[thumb]");
unlink("images/$data[image]");

// remove entry from database
$query = "DELETE FROM $dbimg WHERE id='$id'";
if(mysql_db_query($dbname, $query, $link)) {
// update numimages for that listing
$query = "UPDATE $dbvin SET numimages=numimages-1 WHERE ccode='$ccode'";
mysql_db_query($dbname, $query, $link);

// jump back to summary page
mysql_close($link);
echo "<script language='JavaScript'> window.location='summary.php?ccode=$ccode'; </script>";
exit();
}
@mysql_close($link);
}
?>

Morgenmuffel
15-08-2005, 09:26 AM
Thanks all for clearing that up

I'll give it a go later on today :thumbs: