Jump to content

Archived

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

Bhaal

Unlinking an array of files

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Without knowing how you are storing the list of filenames, there is no way we can help you. Please give a sample.

Ken

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Why are you exploding on the character "|"?

Ken

Share this post


Link to post
Share on other sites
I dunno.  Don't ask.  (Oh well, you already did.)

What do you recommend?

Share this post


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

Share this post


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

Share this post


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

Share this post


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

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.