Jump to content

Archived

This topic is now archived and is closed to further replies.

Jorman

Delete images from directory

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 {
?>

Share this post


Link to post
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.

Share this post


Link to post
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 {
?>

Share this post


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

Share this post


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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.