Jump to content

Delete images from directory


Jorman

Recommended Posts

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 {
?>
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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 {
?>
Link to comment
Share on other sites

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.

[code]
<?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";}
    }
    
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.