ashrobbins87 Posted March 25, 2009 Share Posted March 25, 2009 I am trying to display the contents of a folder and place a delete button next to each file. Then I want to be able to press the button and delete the file, but instead, when I refresh the page to view my changes, all of the files are deleted even though I dont touch the buttons. The code is below... <?php if ($handle = opendir("../Images/")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $myfile = "../Images/".$file; echo "<a href=\"../Images/".$file."\">".$file."</a>"; echo "<img src=\"../Images/".$file."\" width=\"70\" height=\"50\">"; echo"<input type=\"button\" value=\"Delete\" onClick=\""; unlink($myfile); "\"><br />"; } } closedir($handle); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/ Share on other sites More sharing options...
POG1 Posted March 25, 2009 Share Posted March 25, 2009 This line is the error: echo"<input type=\"button\" value=\"Delete\" onClick=\""; unlink($myfile); "\"><br />"; It's not printing the html after the unlink function.. echo '<input type="button" value="Delete" onClick="'.unlink($myfile).'"><br />'; Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/#findComment-793693 Share on other sites More sharing options...
JonnoTheDev Posted March 25, 2009 Share Posted March 25, 2009 echo '<input type="button" value="Delete" onClick="'.unlink($myfile).'"><br />'; This is not the issue at all. You cannot call a php function within a javascript event handler. All you are doing is looping through the files and the function unlink() will remove each file. You should replace with a hyperlink that calls an action when clicked. Example code i.e. <?php // user has clicked a delete hyperlink if($_GET['action'] && $_GET['action'] == 'delete') { unlink($_GET['filename']); header("Location:files.php"); exit(); } ?> <a href="files.php?action=delete&filename=xyz.jpg">delete file</a> Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/#findComment-793698 Share on other sites More sharing options...
Brian W Posted March 25, 2009 Share Posted March 25, 2009 first off, you have not trigger, ie something that must be true in order to have it run the delete. You could use a $_GET variable to trigger the deletion... having the function in the link does not work my friend because html/javascript cannot simply execute php if you had if($_GET['delete'] > ''){ unlink("dir/".$_GET['delete']; } then have your links look like this <?php echo '<input type="button" value="Delete" onClick="if(confirm('Are you sure?')){ window.location=\'CHANGE_TO_FILENAME.php?delete='.$myfile.'\'; }"><br />'; ?> that is untested code, so you may have a problem, I don't think you would though. Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/#findComment-793699 Share on other sites More sharing options...
ashrobbins87 Posted March 25, 2009 Author Share Posted March 25, 2009 Ok I'm using Brian's code and have muddled it into something like this... <?php if ($handle = opendir("../Images/")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $myfile = "../Images/".$file; echo "<td><a href=\"../Images/".$file."\"><img src=\"../Images/".$file."\" width=\"70\" height=\"50\"></a></td>"; if($_GET['delete'] > '') { unlink("dir/".$_GET['delete']); } echo "<input type=\"button\" value=\"Delete\" onClick=" if(confirm('Are you sure?')) { window.location="".$myfile."?delete=".$myfile.""; } "><br />"; } } closedir($handle); } ?> Where am I going wrong now? Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/#findComment-793709 Share on other sites More sharing options...
Brian W Posted March 25, 2009 Share Posted March 25, 2009 if(confirm('are you sure')) is js, not php... <?php if ($handle = opendir("../Images/")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $myfile = "../Images/".$file; echo "<td><a href=\"../Images/".$file."\"><img src=\"../Images/".$file."\" width=\"70\" height=\"50\"></a></td>"; if($_GET['delete'] > '') { unlink("dir/".$_GET['delete']); } echo "<input type=\"button\" value=\"Delete\" onClick=\" if(confirm('Are you sure?')) { window.location="".$myfile."?delete=".$myfile.""; } \"><br />"; } } closedir($handle); } ?> All I did was add a slash to the quotes around the if(confirm()) chunk... try that Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/#findComment-793714 Share on other sites More sharing options...
POG1 Posted March 25, 2009 Share Posted March 25, 2009 You are trying to use JS in PHP Try wrapping the JS into a function then add into the JS secion/files of your page. Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/#findComment-793716 Share on other sites More sharing options...
lonewolf217 Posted March 25, 2009 Share Posted March 25, 2009 why are you trying to use JS onClick events for this ? Just have a form around the Delete button and submit the form to the same page to process the $_GET['delete'] event Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/#findComment-793717 Share on other sites More sharing options...
ashrobbins87 Posted March 25, 2009 Author Share Posted March 25, 2009 Parse error: syntax error, unexpected '"', expecting ',' or ';' in C:\wamp\www\Site_Pages\cms\Untitled-1.php on line 24 lonewolf and POG1 thanks for the input, I'm not sure I understand what you're saying tho, I'm really new to this! Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/#findComment-793721 Share on other sites More sharing options...
lonewolf217 Posted March 25, 2009 Share Posted March 25, 2009 put this in your loop that displays each of the files. for each file it will display a Delete button. when pressed it will reload the page and delete that file that was selected <form name="deleteSomething" action="<?php echo $_SERVER['PHP_SELF'].'?delete=true';?> > <input type="button" name="fileToDelete" value="<?php echo $file; ?>"> <input type="submit" value="Delete"> and this goes near the top of your page <?php if(isset($_GET['delete']) && $_GET['delete']=='true') { unlink("/dir/".$_POST['fileToDelete']); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/#findComment-793728 Share on other sites More sharing options...
JonnoTheDev Posted March 25, 2009 Share Posted March 25, 2009 Your code to delete the file should not be contained with the loop. It should be at the very top of the page - before you start printing anything to the screen. Once you have deleted the file reload the page. Check my post above. It would also be a lot easier to use a standard hyperlink to action the delete rather than a form which is not needed as the other posts suggest however it is upto you. Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/#findComment-793739 Share on other sites More sharing options...
ashrobbins87 Posted March 25, 2009 Author Share Posted March 25, 2009 Right I think we're nearly there!! It now deletes a file, but whichever button I press it always deletes the last file in the folder, IOW the one the loop gets to last. <?php if(isset($_GET['delete']) && $_GET['delete']=='true') { unlink("../Images/".$_POST['fileToDelete']); } ?> <?php if ($handle = opendir("../Images/")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $myfile = "../Images/".$file; echo "<td><a href=\"../Images/".$file."\"><img src=\"../Images/".$file."\" width=\"70\" height=\"50\"></a></td>"; echo "<form method=\"post\" name=\"deleteSomething\" action="?><?php echo $_SERVER['PHP_SELF'].'?delete=true';?><?php echo" > <input type=\"text\" name=\"fileToDelete\" value=".$file." > <input type=\"submit\" value=\"Delete\">"; } } closedir($handle); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/#findComment-793747 Share on other sites More sharing options...
Brian W Posted March 25, 2009 Share Posted March 25, 2009 yeah, could do this without the js onclick, but it wouldn't be as cool :-p sorry, I see the problem now <?php if ($handle = opendir("../Images/")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $myfile = "../Images/".$file; echo "<td><a href=\"../Images/".$file."\"><img src=\"../Images/".$file."\" width=\"70\" height=\"50\"></a></td>"; if($_GET['delete'] > '') { unlink("dir/".$_GET['delete']); } echo "<input type=\"button\" value=\"Delete\" onClick=\" if(confirm('Are you sure?')) { window.location='this_page.php?delete=$myfile'; } \"><br />"; } } closedir($handle); } ?> in there where it says "this_page.php", that should be the name of the file you are working on, as in the php file the code is in... basicaly what we are doing is saying, if they confirm they want to delete it, send them to this same page with an additional parameter attached telling it to delete that file. When they get to that page and $_GET['delete'] is specified, then it will delete it... Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/#findComment-793748 Share on other sites More sharing options...
lonewolf217 Posted March 25, 2009 Share Posted March 25, 2009 That is odd. Right click your page and view source. see what the values are for the <input type="button"> each one should have a unique filename which is the same as as the file that was just displayed Right I think we're nearly there!! It now deletes a file, but whichever button I press it always deletes the last file in the folder, IOW the one the loop gets to last. <?php if(isset($_GET['delete']) && $_GET['delete']=='true') { unlink("../Images/".$_POST['fileToDelete']); } ?> <?php if ($handle = opendir("../Images/")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $myfile = "../Images/".$file; echo "<td><a href=\"../Images/".$file."\"><img src=\"../Images/".$file."\" width=\"70\" height=\"50\"></a></td>"; echo "<form method=\"post\" name=\"deleteSomething\" action="?><?php echo $_SERVER['PHP_SELF'].'?delete=true';?><?php echo" > <input type=\"text\" name=\"fileToDelete\" value=".$file." > <input type=\"submit\" value=\"Delete\">"; } } closedir($handle); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/#findComment-793749 Share on other sites More sharing options...
ashrobbins87 Posted March 25, 2009 Author Share Posted March 25, 2009 I think I've solved it. I was being a bit of a spud and forgot to close the form. I put in a </form> tage and it now seems to be working. Thank you so much for all your help guys, there should be a thanks or Rep button on this forum and you could all have some!! Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/#findComment-793763 Share on other sites More sharing options...
ashrobbins87 Posted March 25, 2009 Author Share Posted March 25, 2009 One little niggly thing though, every time the page is refreshed I get the error: Warning: unlink(../Images/Image018.jpg) [function.unlink]: No such file or directory in C:\wamp\www\Site_Pages\cms\Untitled-1.php on line 15 The filename is the file that was first in the loop the last time the page was loaded, even though this file may have been deleted. Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/#findComment-793767 Share on other sites More sharing options...
Brian W Posted March 25, 2009 Share Posted March 25, 2009 after the unlink() part, add header('location: thispage.php'); die(); that will clear the query string. Replace thispage.php with the actual name of that page. Quote Link to comment https://forums.phpfreaks.com/topic/151085-delete-a-file-when-click-button/#findComment-793776 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.