Dsfsdf Dsfsd
Dsfsdf Dsfsd

Reputation: 11

FTP delete and FTP unlink not working

I want to delete a specific file withn an FTP folder using PHP.

Here's the work I have done so far: This is the code I'm using to echo the image view and the checkbox so that users can select an image and then click a delete button.

for ($i = 0; $i < $r; $i++)
{   
    echo "<form action='somephpfile.php' method='post' enctype='multipart/form-data'>"
        . "<img src='$images[$i]' width='50' height='50' />"
        . "<input type='checkbox' name='$i' value='Restaurant' />"
        . $images[$i]
        . "<input type='submit' name='submit' value='Delete' />"
        . "</form>";
}

This is how I try to delete the file that the user wants to delete:

$images = ftp_nlist($conn_id, "somefolder");
$r = count($images);

for ($i = 0; $i < $r; $i++)
{
    if ($selected_checkbox = $_POST['$i'])
    {   
        $myFile = "$images[$i]";
        $fop = fopen($myFile, "w") or die("can not open file");
        $fclose($fop); 
        unlink($fop); 

        if (ftp_delete($conn, $myFile))
        {
            echo "$file deleted successful\n";
        }
        else
        {
            echo "could not delete $file\n";
        }
    }
}

ftp_close($conn);

Why is this example not working?

Upvotes: 1

Views: 1222

Answers (1)

Marius Schulz
Marius Schulz

Reputation: 16440

The following line of code doesn't correctly compare $selected_checkbox with $_POST['$i'] but assigns the posted value instead, which always evaluates as true:

if ($selected_checkbox = $_POST['$i'])

You want to use the comparison operator == instead of the assignment operator =. Take a look at the section Comparion Operators in this Overview of PHP Operators.

Upvotes: 2

Related Questions