Multiple Checkboxes as an Array to Delete Files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Multiple Checkboxes as an Array to Delete Files

Post 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:
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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]
Last edited by ol4pr0 on Thu Jul 21, 2005 3:14 pm, edited 1 time in total.
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Did you say you had printed out $deletethese? Do that right before line 5 and see what you get.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Post by dyonak »

Done, here's the result:

Array ( [0] => imagestest/serenity.jpg [1] => imagestest/thirdlair.jpg )
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

also...does the web user have permission to delete files? :o
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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).
Last edited by pickle on Thu Jul 21, 2005 3:48 pm, edited 1 time in total.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.";
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

try print_r on the post variable itself w/o resetting it.
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
Post Reply