Jump to content

[SOLVED] delete image with unlink


Marcos01

Recommended Posts

Hi All,

 

I use 2 dropdown boxes. The first to select the folder and the 2nd to select the image I want to delete.

So far so good. But when I hit the button to delete the image nothing happens. The selected image is not deleted. And I don't get an error message.

 

What is wrong with my code?

 

if ($_POST['doaction']=='confirm'.'delete') 
{  
unlink('../userfiles/images/projects/'.$_POST["folders"].'/'.$_POST["images"].' ');
    }

Link to comment
Share on other sites

That script is a timebomb waiting to happen. Someone could use it to delete any file on your website by using relative paths in the values!

 

You need to have some very strong, comprehensive validation of those values.

 

But, specifically to address your problem, I'm guessing that if the values are correctly passed then the problem is with that last .' ' that is appended to the end of the path. Why are you adding a space? As far as I know it is impossible to have a file name with a space at the end. So, remove that and add some debugging code to see what is happening until you get the script working.

 

if ($_POST['doaction']=='confirm'.'delete') 
{
    $file = "../userfiles/images/projects/{$_POST['folders']}/{$_POST['images']}";
    if (file_exists($file))
    {
        unlink($file);
    }
    else
    {
        echo "The file '$file' does not exist.";
    }
}

Link to comment
Share on other sites

Than maybe that first IF is never meeting a true condition. Try this:

 

if ($_POST['doaction']=='confirm'.'delete') 
{
    $file = "../userfiles/images/projects/{$_POST['folders']}/{$_POST['images']}";
    if (file_exists($file))
    {
        unlink($file);
        echo "Attempting to delete '$file'";
    }
    else
    {
        echo "The file '$file' does not exist.";
    }
}
else
{
    echo "No post data";
}

Link to comment
Share on other sites

//select directory
$folders = (ftp_nlist($conn,"path to projects"));
echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">';
echo '<select name="folders">';
for ($i=2;$i<count($folders);$i++)
	{
	echo "<option>".$folders[$i]."</option>";
	}
echo '</select>';
echo '<input name="doaction" type="Submit" value="confirm"></form>';

if ($_POST['doaction']=='confirm') 
{

//select image
echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">';
echo '<select name="images">';
for ($i=2;$i<count($images);$i++)
	{
	echo "<option>".$images[$i]."</option>";
	}
echo '</select>';
echo '<input name="doaction" type="Submit" value="delete"></form>';
}
//delete image
if ($_POST['doaction']=='confirm'.'delete') 
{
    $file = "../userfiles/images/projects/{$_POST['folders']}/{$_POST['images']}";
    if (file_exists($file))
    {
        unlink($file);
        echo "Attempting to delete '$file'";
    }
    else
    {
        echo "The file '$file' does not exist.";
    }
}
else
{
    echo "No post data";
}

Link to comment
Share on other sites

Well, the debugging code shows that the problem is not with the unlink() but with your forms. You have TWO different forms. Only ONE form can be submitted!!! So the if condition

$_POST['doaction']=='confirm'.'delete'

will never return true.

 

Try this:

<?php

//select directory
//$folders = (ftp_nlist($conn,"path to projects"));

$folders = array('one','two','three','four','five');

echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">';

//Create Folders select list
echo "<select name=\"folder\">\n";
for ($i=2;$i<count($folders);$i++)
{
    $selected = ($folders[$i]==$_POST['folder'])?' selected="selected"':'';
    echo "<option value=\"{$folders[$i]}\"$selected>{$folders[$i]}</option>\n";
}
echo "</select>\n";

echo '<input name="doaction" type="Submit" value="select_folder"></form>';


if ($_POST['doaction']=='select_folder' || $_POST['doaction']=='delete_image') 
{
    //There should be some code here to
    //get the image names for the selected folder

    //Select image to delete
    echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">\n";
    echo "<input type=\"hidden\" name=\"folder\" value=\"{$_POST['folder']}\">\n";
    echo "<select name=\"images\">\n";
    for ($i=2;$i<count($images);$i++)
    {
        echo "<option>{$images[$i]}</option>\n";
    }
    echo "</select>\n";
    echo "<input name=\"doaction\" type=\"Submit\" value=\"delete_image\">\n";
    echo "</form>\n";
}

//delete image
if ($_POST['doaction']=='delete_image') 
{
    $file = "../userfiles/images/projects/{$_POST['folders']}/{$_POST['images']}";
    if (file_exists($file))
    {
        unlink($file);
        echo "Deleted '$file'";
    }
    else
    {
        echo "The file '$file' does not exist.";
    }
}

?>

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.