Current location: Hot Scripts Forums » Programming Languages » PHP » Deleting multiple files using checkboxes


Deleting multiple files using checkboxes

Reply
  #1 (permalink)  
Old 07-20-06, 06:46 PM
trailness trailness is offline
New Member
 
Join Date: Jul 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Deleting multiple files using checkboxes

Hi all! I'm a newbie trying to figure out some basic file management code. So far I've gotten an upload script to work, and an open-up-the-text-file and append data script is also working. But I'm stumped on deleting files - after all my Googling, I still haven't found the answer to my problem.

My checkboxes look like this:
Code:
<input type="checkbox" name="deletewhich[1]" value="bracelet.jpg" />
<input type="checkbox" name="deletewhich[2]" value="earrings.jpg" />
<input type="checkbox" name="deletewhich[3]" value="necklace.jpg" />
This is the code that's supposed to delete the files that are checked:
PHP Code:

<?php

$listvals
=$HTTP_POST_VARS['deletewhich'];
$i=count($listvals);
for(
$i=0$i<count($listvals); $i++) {
    if (
$HTTP_POST_VARS["deletewhich".$i] == "on") {
        
$myFile $_FILES['deletewhich'.$i]['name'];
        
$fh fopen($myFile"r") or die("No such file.");
        
fclose($fh);

        
unlink($myFile);
        echo 
"<font face=\"arial,helvetica\" size=\"3\">File <font color=red>$myFile</font> successfully deleted.<br>";
    }
    else {
        echo 
"<font face=\"arial,helvetica\" size=\"3\">Sorry, an error occured. Nothing was deleted.<br>";
    }
}
?>

When I run the code, all I get is my error message - nothing was deleted. Thanks in advance for any suggestions!

Last edited by nico_swd; 07-21-06 at 02:07 AM.
Reply With Quote
  #2 (permalink)  
Old 07-20-06, 08:47 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
The first problem is that your IF(...) statement - if ($HTTP_POST_VARS["deletewhich".$i] == "on") { - won't ever be true because you have specified values value="bracelet.jpg" in the form. Instead of "on" you will receive the values "bracelet.jpg", "earrings.jpg", and "necklace.jpg"

To illustrate this, put the following code in your .php file to display all the $_POST values -
PHP Code:

echo "<pre>";

print_r($_POST);
echo 
"</pre>"
Secondly, I don't think the ".$i] syntax in $HTTP_POST_VARS["deletewhich".$i] is correct, but I could be wrong as the HTTP_POST_VARS is older syntax. If this was using the $_POST global variable, the correct syntax would be something like this -
PHP Code:

$_POST['deletewhich']['1']

$_POST['deletewhich']['2']
$_POST['deletewhich']['3']... 
Lastly, unless this is part of your upload file script, the $_FILES variable will not be set to anything (why upload a file, then deleted it) and the following line does not make sense and is probably not doing anything -
PHP Code:

$myFile $_FILES['deletewhich'.$i]['name']; 

Edit: If you want to see/find out more about what is happening good/bad during the execution of your php script, put the following line near the start of your php file -
PHP Code:

error_reporting(E_ALL); 

This is especially useful when debugging code or when just learning.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???

Last edited by mab; 07-20-06 at 08:52 PM.
Reply With Quote
  #3 (permalink)  
Old 07-21-06, 03:59 PM
trailness trailness is offline
New Member
 
Join Date: Jul 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Correct code

Thank you so much for your help! Between your advice and some trial and error, here is the working script, in case it's useful for someone else:

PHP dynamic form:

PHP Code:

<form action="modify.php" method="post" enctype="multipart/form-data" name="deleteform" id="deleteform">

    <?php
    
if ($handle opendir('.')) {
       
$i=0;
       
$session "sess_".session_id();
       while (
false !== ($file readdir($handle))) {
        if (
$file != "." && $file != ".." && $file != "modify.php") {
        echo 
"input type=\"checkbox\" name=\"deletewhich[$i]\" value=\"$file\" /> $file<br>\r\n";
        
$i++;
           }
       }
       
closedir($handle);
    }
    
?>
    <br>
    <input name="delete" type="hidden" id="delete" />
    <input type="submit" name="submitdelete" value="Delete" />
(This outputs the following code):
Code:
<form action="modify.php" method="post" enctype="multipart/form-data" name="deleteform" id="deleteform">
<input type="checkbox" name="deletewhich[0]" value="earrings.jpg" /> earrings.jpg<br>
<input type="checkbox" name="deletewhich[1]" value="necklace.jpg" /> necklace.jpg<br>
<br>
<input name="delete" type="hidden" id="delete" />
<input type="submit" name="submitdelete" value="Delete" />
</form>
PHP delete script:
PHP Code:

if(isset($HTTP_POST_VARS['delete'])) {

    foreach (
$_POST['deletewhich'] as $myFile) {
        
$fh fopen($myFile"r") or die("No such file."); 
        
fclose($fh);
        
unlink($myFile);
        echo 
"<font face=\"arial,helvetica\" size=\"3\">File <font color=red>$myFile</font> successfully deleted.<br><br>";
    }


Last edited by nico_swd; 07-21-06 at 04:12 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
deleting multiple records from MYSQL database using PHP sujata_ghosh PHP 5 06-04-09 03:07 AM
Download multiple files Bas Steijvers JavaScript 1 06-15-05 02:24 PM
Multiple Resource Files? tim8w Visual Basic 1 03-25-05 07:10 PM
[php] deleting multiple files in directory xeoHosting PHP 1 01-19-04 08:06 AM
multiple Text files to html. andreas66 JavaScript 4 06-13-03 08:04 AM


All times are GMT -5. The time now is 07:22 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.