Jump to content


Photo

Unlinking an array of files


  • Please log in to reply
11 replies to this topic

#1 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 30 October 2006 - 02:34 AM

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:

$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());
     }

('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.

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 30 October 2006 - 04:33 AM

Without knowing how you are storing the list of filenames, there is no way we can help you. Please give a sample.

Ken

#3 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 30 October 2006 - 04:57 AM

"...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?

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 30 October 2006 - 06:16 AM

I asked that question due to this line:
<?php
$MyImages2 = explode("|", $aql[filename]);
?>
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:
<?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());
?>

Ken

#5 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 30 October 2006 - 02:05 PM

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?

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 30 October 2006 - 02:55 PM

Why are you exploding on the character "|"?

Ken

#7 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 30 October 2006 - 03:59 PM

I dunno.  Don't ask.  (Oh well, you already did.)

What do you recommend?

#8 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 31 October 2006 - 12:57 AM

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:

$q1 = "select * from MyTable where gallery=$nid";
$r1 = mysql_query($q1) or die("Problem with the query: $q1<br>" . mysql_error());

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!



#9 jsladek

jsladek
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationFolcroft

Posted 31 October 2006 - 01:19 AM

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);
}

PHPFreaks - I'm addicted, I spend more time reading posts than I do writing code.

#10 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 31 October 2006 - 02:16 AM

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.

#11 jsladek

jsladek
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationFolcroft

Posted 31 October 2006 - 03:12 AM

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);
}

PHPFreaks - I'm addicted, I spend more time reading posts than I do writing code.

#12 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 31 October 2006 - 05:23 AM

Wow - I think this is working!

Thanks!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users