Marcos01 Posted September 29, 2008 Share Posted September 29, 2008 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"].' '); } Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 29, 2008 Share Posted September 29, 2008 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."; } } Quote Link to comment Share on other sites More sharing options...
Marcos01 Posted September 29, 2008 Author Share Posted September 29, 2008 Hello mjdamato, Thanks for your input. I posted ' ' so you can see that it are 2 seperate ' I tried your code but still nothing happens. No error either. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 29, 2008 Share Posted September 29, 2008 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"; } Quote Link to comment Share on other sites More sharing options...
Marcos01 Posted September 29, 2008 Author Share Posted September 29, 2008 It echo's: No post data Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 29, 2008 Share Posted September 29, 2008 show the form please.......... Quote Link to comment Share on other sites More sharing options...
Marcos01 Posted September 29, 2008 Author Share Posted September 29, 2008 //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"; } Quote Link to comment Share on other sites More sharing options...
Marcos01 Posted September 29, 2008 Author Share Posted September 29, 2008 Anyone? Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 29, 2008 Share Posted September 29, 2008 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."; } } ?> Quote Link to comment Share on other sites More sharing options...
Marcos01 Posted October 8, 2008 Author Share Posted October 8, 2008 Thanks alot mjdamato I got it to work Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.