Jump to content


Photo

Delete images from directory


  • Please log in to reply
5 replies to this topic

#1 Jorman

Jorman
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 20 April 2006 - 01:02 PM

Hi All,

I am not a die-hard php programmer, and ik cannot get this script to work... i have searched this forum and the web, but cannot find exactly what i need. I have made a website with an easy to use CMS. The problem is that when i delete a product, i already achieved to remove the database entry (product and photos) BUT i would like te delete the real image files also... this is what i have got so far.... what am i doing wrong?

<?php
if ($delete == "yes") {
mysql_query("DELETE FROM producten WHERE prodid=($productid)");
$result2=mysql_query("SELECT * FROM productfotos WHERE prodid=($prodid)");
$filedir = '/var/www/html/test/images/productfotos';
$dh = opendir($filedir);
unlink($dh.'/'.$result2);
closedir($dh);
mysql_query("DELETE FROM productfotos WHERE prodid=($productid)");
?>
<b><font color="white">Het product is verwijderd!</font></b><br />
<input type=button value="Window Sluiten" onClick="javascript:opener.location.reload(true);self.close();">
<?php
}
else {
?>

#2 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 20 April 2006 - 01:13 PM

you're on the right lines, however...

unlink($dh.'/'.$result2); // this will not work.

$result2 is the variable for the result of your query. So $result2 will equal either 1 or nothing.


you need to have something like:

$num = mysql_num_rows($result2);
for($i=0;$i<$num;$i++)
{
$filename = mysql_result($result,$i,"filename");
unlink($dh.'/'.$filename);
}

Basically... get the filename of the image from your database, and use that as the variable for deletion rather than $result2.

You might need further tinkering, but that is certainly part of your problem.

wisewood: proven fact, I am both wise, and wooden.

#3 Jorman

Jorman
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 20 April 2006 - 01:44 PM

Thnx for the quick reply!!

i understand what you mean, and i have put the code in my page... this is the error i recieve

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/test/cms/delete.php on line 31

Warning: closedir(): supplied argument is not a valid Directory resource in /var/www/html/test/cms/delete.php on line 37

And this is my code...
--------------------------

<?php
if ($delete == "yes") {
mysql_query("DELETE FROM producten WHERE prodid=($productid)");
$result2=mysql_query("SELECT * FROM productfotos WHERE prodid=($prodid)");
$num = mysql_num_rows($result2);
for($i=0;$i<$num;$i++)
{
$filename = mysql_result($result,$i,"filename");
unlink($dh.'/'.$filename);
}
closedir($dh);
mysql_query("DELETE FROM productfotos WHERE prodid=($productid)");
?>
<b><font color="white">Het product is verwijderd!</font></b><br />
<input type=button value="Window Sluiten" onClick="javascript:opener.location.reload(true);self.close();">
<?php
}
else {
?>

#4 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 20 April 2006 - 02:31 PM

i'm currently working on something for you... but i just noticed in your mysql queries you have WHERE whatever = ($variable)

should these brackets really be there? I've never seen anyone use brackets in this way before.
wisewood: proven fact, I am both wise, and wooden.

#5 Jorman

Jorman
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 20 April 2006 - 02:35 PM

Brackets removed, uploaded en tested...still same error

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/test/cms/delete.php on line 31



#6 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 20 April 2006 - 02:52 PM

This is fully tested and works with my test data. You'll need to chop and change a few variables and field names etc but it does the job. I created to test entries in a database, and two associated images in a directory called images.

When i ran the script with the $prodid variable set to something valid in the database, the file, and the database record were deleted.

<?php

// Ignore this bit
// Just connecting to the database
include('../intranet/projects/test_datacon.php');

// For this example i'm using static variables
// Yours will be called from elsewhere.

$productid = "1235";
$filedir = 'images';

$query1 = "SELECT * FROM products WHERE prodid = $productid";
$result1 = mysql_query($query1);
$num = mysql_num_rows($result1);
    for($i=0;$i<$num;$i++)
    {
        $image = mysql_result($result1,$i,"image");
        $prodid_query = mysql_result($result1,$i,"prodid");
        
        unlink("$filedir/$image");
        $delete = "DELETE FROM products WHERE prodid = $prodid_query";
        $result2 = mysql_query($delete);
        if($result2==1) { echo "Record & Image were deleted";}
    }
    
?>

wisewood: proven fact, I am both wise, and wooden.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users