Jorman Posted April 20, 2006 Share Posted April 20, 2006 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 { ?> Quote Link to comment https://forums.phpfreaks.com/topic/7944-delete-images-from-directory/ Share on other sites More sharing options...
wisewood Posted April 20, 2006 Share Posted April 20, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/7944-delete-images-from-directory/#findComment-28949 Share on other sites More sharing options...
Jorman Posted April 20, 2006 Author Share Posted April 20, 2006 Thnx for the quick reply!!i understand what you mean, and i have put the code in my page... this is the error i recieveWarning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/test/cms/delete.php on line 31Warning: closedir(): supplied argument is not a valid Directory resource in /var/www/html/test/cms/delete.php on line 37And 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 { ?> Quote Link to comment https://forums.phpfreaks.com/topic/7944-delete-images-from-directory/#findComment-28959 Share on other sites More sharing options...
wisewood Posted April 20, 2006 Share Posted April 20, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/7944-delete-images-from-directory/#findComment-28983 Share on other sites More sharing options...
Jorman Posted April 20, 2006 Author Share Posted April 20, 2006 Brackets removed, uploaded en tested...still same errorWarning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/test/cms/delete.php on line 31 Quote Link to comment https://forums.phpfreaks.com/topic/7944-delete-images-from-directory/#findComment-28984 Share on other sites More sharing options...
wisewood Posted April 20, 2006 Share Posted April 20, 2006 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 databaseinclude('../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] Quote Link to comment https://forums.phpfreaks.com/topic/7944-delete-images-from-directory/#findComment-28990 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.