Bhaal Posted October 30, 2006 Share Posted October 30, 2006 Hi.I'm trying to delete (unlink) multiple files. Problem is, only one of the files is being deleted.Here's the query I have:[code] $sql = "select * from ps_images where gallery=$nid";$rql = mysql_query($sql) or die(mysql_error());if(mysql_num_rows($rql) > '0') { $aql = mysql_fetch_array($rql); if(!empty($aql[filename])) { $MyImages2 = explode("|", $aql[filename]); while(list(,$v2) = each($MyImages2)) { unlink("../thumbs/$v2"); } } } $q3 = "delete from ps_images where gallery=$nid"; mysql_query($q3) or die(mysql_error()); }[/code]('filename' is the name of the field that contains the image name.)So, why does this only delete one file? I think it's the 'explode' function and the delineator, but what do I know?Any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 30, 2006 Share Posted October 30, 2006 Without knowing how you are storing the list of filenames, there is no way we can help you. Please give a sample.Ken Quote Link to comment Share on other sites More sharing options...
Bhaal Posted October 30, 2006 Author Share Posted October 30, 2006 "...storing the list of filenames..."What? I don't quite get what that means. I'm not storing a "list of filenames". There are a bunch of files (thumbnail graphics, actually) that reside in a subdir called "thumbs" (hence the path in 'unlink("../thumbs/$v2")'). These files are copied to the server and the file name is stored in a field called "filename" from within a different form.The form I'm inquiring about deletes these files. Since there are multiple files to delete (and multiple records - each record contains a thumbnail graphic), I thought that putting them in an array and deleting them with a 'while' loop would work. But it's only deleting one file.Does that help? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 30, 2006 Share Posted October 30, 2006 I asked that question due to this line:[code]<?php$MyImages2 = explode("|", $aql[filename]);?>[/code]This tells me that the field "filename" contains a string with the format "something|filename".It looks like you have the while is the wrong place, try:[code]<?php$sql = "select * from ps_images where gallery=$nid";$rql = mysql_query($sql) or die("Problem with the query: $sql<br>" . mysql_error());while ($aql = mysql_fetch_assoc($rql)) { if(!empty($aql[filename])) { $MyImages2 = explode("|", $aql[filename]); unlink('../thumbs/' . $MyImages[1]); } } $q3 = "delete from ps_images where gallery=$nid"; mysql_query($q3) or die(mysql_error());?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
Bhaal Posted October 30, 2006 Author Share Posted October 30, 2006 Well - each record in the table has a field called 'filename' which stores one and only one graphic file. (So, if there are 10 records in the table, there are 10 graphics.)I'm pretty sure that "|" char in the the explode function is messing it up. Is there a better approach to unlinking a bunch of graphic? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 30, 2006 Share Posted October 30, 2006 Why are you exploding on the character "|"? Ken Quote Link to comment Share on other sites More sharing options...
Bhaal Posted October 30, 2006 Author Share Posted October 30, 2006 I dunno. Don't ask. (Oh well, you already did.)What do you recommend? Quote Link to comment Share on other sites More sharing options...
Bhaal Posted October 31, 2006 Author Share Posted October 31, 2006 Perhaps I should start over and ask in a semi-generic fashion.How does one delete (unlink) a 'range' of graphic files?THE SCENARIO:There is a mySQL table - let's call it MyTable.This table has a field called MyThumb. This field holds the actual name of a graphic image. (The actual graphic file is uploaded to the server and exists in a subdir called /thumbs.)I need to delete records from MyTable based on a range - and that range can be found by searching on a field called 'gallery'. Therefore, I also need to delete the actual graphic image files that correspond with each of these found records.For example, this code will find a range of records:[code]$q1 = "select * from MyTable where gallery=$nid";$r1 = mysql_query($q1) or die("Problem with the query: $q1<br>" . mysql_error());[/code]It will also, therefore, find a 'range' of graphic image file names.If this found range (from the code above) contains 3 records, it will contain 3 values from the field MyThumb.These 3 values are the actual names of the graphics that have been uploaded to the server. For example, these names could be: MyImage1.jpg, MyImage2.jpg and MyImage3.jpg.I know how to actually delete the records (easy enough) but how do I delete (unlink) the actual graphic files contained in this range? How do I delete MyImage1.jpg, MyImage2.jpg and MyImage3.jpg from the server?I assume that an array of the image names would be used - but I just can't get it to work.I truly hope this makes the problem clearer. Any help would be greatly appreciated.Thanks! Quote Link to comment Share on other sites More sharing options...
jsladek Posted October 31, 2006 Share Posted October 31, 2006 Here is a little script I use to delete everything out of a directory.<?foreach (glob("test/*.*") as $filename){ unlink($filename);}print("files deleted<br>");print("<a href='phpg.php'>Try Again.");?>I would expect somthing like this would work....$sql = "select * from ps_images where gallery=$nid";$result = mysql_query($sql);while($row = mysql_fetch_array($result)) { $filename = "../thumbs/".$row[filename] unlink($filename);} Quote Link to comment Share on other sites More sharing options...
Bhaal Posted October 31, 2006 Author Share Posted October 31, 2006 Thanks for the help, jsladek.Unfortunately, deleting everything is not what I need. I need to delete only those files that are found within a range, as explained in a previous message. Quote Link to comment Share on other sites More sharing options...
jsladek Posted October 31, 2006 Share Posted October 31, 2006 That is what the code I suggest (might need slight mod) should do...As it retreives each record it deletes the file associated with it....$sql = "select * from ps_images where gallery=$nid";$result = mysql_query($sql);while($row = mysql_fetch_array($result)) { $filename = "../thumbs/".$row[filename]; unlink($filename);} Quote Link to comment Share on other sites More sharing options...
Bhaal Posted October 31, 2006 Author Share Posted October 31, 2006 Wow - I think this is working!Thanks! Quote Link to comment 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.