Page 1 of 2

Multiple Checkboxes as an Array to Delete Files

Posted: Thu Jul 21, 2005 1:21 pm
by dyonak
Ok multiple checkboxes being created with a bunch of thumbnails. Here's the form:

Code: Select all

<input type='checkbox' name='deletetheseї]' value='$filename'>
Here's the form handler:

Code: Select all

//Handle the deletion form
$count = 0;
if (isset($_POST['delsubmit'])) {
	$deletethese=$_POST['deletethese'];
	foreach ($deletethese as $deletethis) {
		$count ++;
		unlink($deletethis);
	}
echo $count.' images deleted.';
}
It's not working, doesn't delete the files and always reads "0 images deleted."

Help please! :oops:

Posted: Thu Jul 21, 2005 3:08 pm
by ol4pr0
How about something like this.

Code: Select all

function _delete( $id ) {
	global $dbc;#database connect.. change to own settings.
	echo "DELETE FROM DATABASE WHERE id IN ($ids)";
	if (!is_array( $id ) || count( $id ) < 1) {
		echo "<script> alert('Seleccione el artículo para suprimir'); window.history.go(-1);</script>n";
		exit;
	}
	if (count( $id )) {
		$ids = implode( ',', $id );
		$dbc->setQuery( "DELETE FROM DATABASE WHERE id IN ($ids)" );
		if (!$dbc->getError( )) {
# my db error Return change to your own settings
			echo "<script> alert('".$dbc->getError( )."');
			window.history.go(-1); </script>n";
		}
	}
}
_delete(array $POST checkbox);
EDIT:
Sorry my Bad.. ill leave the code but here for file handling

Code: Select all

for ($i=0;$i<sizeof($CHECKBOX_VALUE);$i++);
unlink($CHECKBOX_VALUE[$i]);
Havent tested this.. but i suppose it should work.

[/b]

Posted: Thu Jul 21, 2005 3:14 pm
by dyonak
The file isn't in a database, it's just residing in a folder. I know I need to be using unlink but I think something in my foreach statement isn't populating the $deletethis variable properly. I also have a print_r ($deletethese); below all of this code and it does work properly. All of the checkboxes I select are populating the array so I know that's correct, they just don't delete.

Posted: Thu Jul 21, 2005 3:25 pm
by pickle
3 things:
  1. Try echoing $deletethis as you're deleting it. Maybe the filename's wonky.
  2. Is $deletethis a filename or is it a fully qualified file path? unlink() needs a fully qualified file path to work.
  3. unlink()ing files directly from $_POST is VERY bad. I could easily just send my own POST variables with "/var/lib/mysqld" as a filename and BAM! - the mysql executable is gone.

Posted: Thu Jul 21, 2005 3:33 pm
by dyonak
pickle,

It seems the problem lies before the unlink(). I have echo'd the $count and it always shows zero, the count incrementing should happen right before the deletion.

$deletethis is not a fully qual file path, I can fix that easily enough though. I did try echoing $deletethis and got nada.

Any other ideas?

Posted: Thu Jul 21, 2005 3:35 pm
by pickle
Did you say you had printed out $deletethese? Do that right before line 5 and see what you get.

Posted: Thu Jul 21, 2005 3:37 pm
by dyonak
Done, here's the result:

Array ( [0] => imagestest/serenity.jpg [1] => imagestest/thirdlair.jpg )

Posted: Thu Jul 21, 2005 3:39 pm
by Burrito
also...does the web user have permission to delete files? :o

Posted: Thu Jul 21, 2005 3:44 pm
by dyonak
Yes, but I'm pretty sure at this point it doesn't matter. I've changed my code to troubleshoot small bits at a time and found that. .

Code: Select all

$count = 0;
if (isset($_POST['delsubmit'])) {
	$deletethese = $_POST['deletethese'];
	foreach ($deletethese as $deletethis) {
		$count ++;
	}

}
echo "$count images deleted.";
always yields
0 images deleted.
as an output.

I don't even attempt to unlink at this point, I can add that in when I figure out what's causing my $count to not increment.

Posted: Thu Jul 21, 2005 3:47 pm
by pickle
Wow - I've pretty much run out of debugging ideas. Try changing your foreach statement to read:

Code: Select all

foreach($deletethese as $index=>$deletethis)
If you print out $deletethese right before the foreach loop, get stuff, and print out $deletethis in the loop and get nothing, then I'm at a loss.

Edit: Also, I think the syntax is $count++, not $count ++ (notice the lack of space).

Posted: Thu Jul 21, 2005 3:48 pm
by Burrito
good that you're picking it apart...that's the best way to troubleshoot:

try this and see if it yields anything:

Code: Select all

$count = 0;
$fdelete = "";
if (isset($_POST['delsubmit'])) {
    foreach ($_POST['deletethese'] as $deletethis) {
        $fdelete .= $deletethis."&nbsp;";
        $count ++;
    }
 
}
echo $fdelete."<br>";
echo "$count images deleted.";

Posted: Thu Jul 21, 2005 3:54 pm
by dyonak
Scratch what I said before, I had put the print_r ($deletethese); after the closing brackets, not thinking it would make a difference. I tried the following code, just like you suggested. . .

Code: Select all

$count = 0;
if (isset($_POST['delsubmit'])) {
	$deletethese = $_POST['deletethese'];
	print_r ($deletethese);
	foreach($deletethese as $index=>$deletethis){
		$count ++;
	}

}
echo "$count images deleted.";
And it did NOT print the array, why?! I also tried using print_r ($deletethese); after the echo for quantity of imgs deleted and it DID print the array, I'm even more confused but hopefully this helps.

Posted: Thu Jul 21, 2005 3:57 pm
by Burrito
try print_r on the post variable itself w/o resetting it.

Posted: Thu Jul 21, 2005 4:01 pm
by dyonak
Tried print_r ($_POST['deletethese']); in both spots (above the other print_r and down below out of the loop) neither printed the array.

Posted: Thu Jul 21, 2005 4:04 pm
by Burrito
pick it apart even smaller.

try print_r at the top of the page before you even get into looping...

also try echoing the post var name and see if it's coming across as an array.