So i have created a list of all the files that are in a certain folder, and in the right hand is a checkbox.

echo "<form action='checkbox-form.php' method='POST' name='MyForm'>";

and this is the part of interest

    $c +=1;
    <td > <input type='checkbox' name='formImage[]' value='$c'/>&nbsp;<br /> </td>

So how can i do it?

Recommended Answers

All 20 Replies

if(isset($_POST['formImage']) && !empty($_POST['formImage'])) {

    // do what you like with it
}

ok and how can i say: take all the values that formImage has, and for each and every one of it, if it is checked then delete file

this is the checkbox-form.php

<?PHP
    if(isset($_POST['formImage']) && !empty($_POST['formImage'])) {
        $a = $_POST['formImage'];
        $N = count($a);
        echo("<p>You selected $N image(s): ");
        for($i=0; $i < $N; $i++){
            echo($a[$i] . " ");
        }
        echo("</p>");
    }
?>

so it shows me the nr of the image that has been checked. Now how to delete it?

To delete files from the filesystem you have to use unlink command:

for($i=0; $i < $N; $i++){
    echo($a[$i] . " ");
    unlink($a[$i]);
}

You might have to construct the path to the files first, if the file is not in the current directory. Also permissions have to be correct to delete.

Or to make sure file exists:

for($i=0; $i < $N; $i++){
    echo($a[$i] . " ");
    if(file_exists($a[$i])) {
        unlink($a[$i]);
    } else {
        echo "Could not delete file {$a[$i]}<br />";
    }
}

when i run it it says line 8 meaning unlink($a[$i]); no such file or directory

Can you post the snippet of code that causes troubles?

You mean the this

<?PHP
    if(isset($_POST['formImage']) && !empty($_POST['formImage'])) {
        $a = $_POST['formImage'];
        $N = count($a);
        for($i=0; $i < $N; $i++){
            for($i=0; $i < $N; $i++){
                echo($a[$i] . " ");
                unlink($a[$i]);
            }
        }
    }
?>

or this

$dir =opendir("countries/$folder/$sfolder");
$handle ="countries/$folder/$sfolder" ;

echo "<form action='checkbox-form.php' method='POST' name='MyForm'>";
echo "<table cellpadding='5px'>";
echo "<tr ><th colspan='3' align='center'> $folder : $sfolder <br /><br /></th>";
echo "</tr>";
echo    "<tr>
            <td align='center'> File </td>
            <td align='left'> Delete </td>
            <td align='center'> Image </td>
        </tr>";
$c=0;
while ((($file = readdir($dir)) !== false) && ($c <1))       //se sare
    {$c++;}
$c=0;
while ((($file = readdir($dir)) !== false) && ($file!="Thumbs.db")){
     $c+=1;
     echo "<tr>
            <td width='400'> $file </td>
            <td > <input type='checkbox' name='formImage[]' value='$c'/>&nbsp;<br /> </td>
            <td> <img src='{$handle}/{$file}' style='width:50px; height:50px;' />
          </tr>";
    }
closedir($dir);
echo "  <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td ><br /><input type='submit' name='submit' value='Delete' /></td>
        </tr>";
echo "</table>";
echo "</form>";

It could be that the first snippet is playing up. Why do you have two for loops, if only one would do? And sticka an if(file_exists($a[$i])) check before deleting:

if(isset($_POST['formImage']) && !empty($_POST['formImage'])) {
    $a = $_POST['formImage'];
    $N = count($a);
    for($i=0; $i < $N; $i++){
        // check first if file exists
        if(file_exists($a[$i])) {
            // delete it only if it exists
            unlink($a[$i]);
        } else {
            // if it doesnt exist display an error message
            echo "Could not delete file {$a[$i]}<br />";
        }
    }
}

do i need the path to file? because if i do i tried echo "<form action='checkbox-form.php?folder=$folder&sfolder=$sfolder' method='POST' name='MyForm'>"; and in other php if i say $y=$_POST['folder'] it says undefined index ....

What is actualy in the $_POST array and consequently in the $a? Could you put ththe following debug line in and post the result?

if(isset($_POST['formImage']) && !empty($_POST['formImage'])) {
    //DEBUG
    die(print_r($_POST, 1));

    ...

Array ( [formImage] => Array ( [0] => 1 ) [submit] => Delete )

Sorry, I lead you on the wrong track. The $_POST array contains only the values (1) of the checked checkboxes. Give me a minute to study your code, I'll be back.

The checkbox names should have values equal to filenames so you get the array of filenames to delete in $_POST:

echo "<tr>
    <td width='400'> $file </td>
    <td > <input type='checkbox' name='formImage[]' value='$file' />&nbsp;<br /> </td>
    <td> <img src='{$handle}/{$file}' style='width:50px; height:50px;' />
    </tr>";

I optimized the code for deleting a bit:

if(isset($_POST['formImage']) && !empty($_POST['formImage'])) {

    foreach($_POST['formImage'] as $filename) {

        // check first if file exists
        if(file_exists($filename)) {

            // delete it only if it exists
            unlink($filename);

        } else {

            // if it doesnt exist display an error message
            echo "Could not delete file $filename}<br />";
        }
    }
}

Sory to missled you in my previous post. It was because I was replying on two similar posts and have mixed things up.

file_exist and unlink shouldn't have the whole path?

I haven't tested the deleting itself. Judging by example here the filename is enough if the file is in the same directory as the script. You can try both versions (without and with the path) and see if they work. If you have troubles I can do a test but not before tomorrow night (it is just past midnight here now |-)).

got it. I finally did it, but i have a little problem... when it finishes deleting the files it remains in the checkbox-form.php, and i tried with exit and die to get out of there but no succes, any ideas?

Use header function at the end. But be careful, no HTML or any other output should be sent before the header function in order for it to work:

header('location:yourpage.php');
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.