richard_PHP Posted March 8, 2009 Share Posted March 8, 2009 hello all. absolute novice to unlink so the code will probably appear a load of rubbish. anyway.. ive listed the files from a directory and want to have it so each file is displayed with a radio button, clickable, and a submit button at the end of the form. all this so that whatever file is chosen then you can delete the file from the directory. which is where unlink comes into it. BUT.. it either doesnt work or i get an error, like i said, absolute novice using this function so i cant really find my problem. codes: files.php (directory listing) <form action="remove.php" method="post" name="files"> <? //define the path as relative $path = "uploads/"; //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); //running the while loop while ($file = readdir($dir_handle)) { if($file!="." && $file!="..") echo "<input name='$file' type='radio' value='' /><a href='uploads/$file' target='_blank'>$file</a><br />"; } //closing the directory closedir($dir_handle); ?> <p><input name="submit" type="submit" value="Delete File" /></p> </form> remove.php ('you have deleted a file' type page) <?php $file = $_POST['files']; if (file_exists('$file')){ unlink("$file"); echo "Did it work?"; } else { echo "Oops"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148462-solved-unlink-help/ Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 What is the error message? Quote Link to comment https://forums.phpfreaks.com/topic/148462-solved-unlink-help/#findComment-779503 Share on other sites More sharing options...
richard_PHP Posted March 8, 2009 Author Share Posted March 8, 2009 the original one was before i added the if statement and it said: Warning: unlink() [function.unlink]: No error in C:\xampp\htdocs\FYP - Final Year Project\website\remove.php on line 25 but then adding the if statement, it just runs the else message. Quote Link to comment https://forums.phpfreaks.com/topic/148462-solved-unlink-help/#findComment-779504 Share on other sites More sharing options...
RussellReal Posted March 8, 2009 Share Posted March 8, 2009 if (file_exists('$file')) { is where your problem is ' single quotes do not evaluate variables, use double quotes or no quotes at all.. you don't need to quote variables. if (file_exists($file)){ Quote Link to comment https://forums.phpfreaks.com/topic/148462-solved-unlink-help/#findComment-779505 Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 You can not catch $_POST['files'] it does not exisit because you are using a form that generates dynamic names. Instead of naming the files that way you need to make that the value of files. Like this. <form action="remove.php" method="post" name="files"> <? //define the path as relative $path = "uploads/"; //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); //running the while loop while ($file = readdir($dir_handle)) { if($file!="." && $file!="..") echo "<input name='files' type='radio' value='$file' /><a href='uploads/$file' target='_blank'>$file</a><br />"; } //closing the directory closedir($dir_handle); ?> <p><input name="submit" type="submit" value="Delete File" /></p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/148462-solved-unlink-help/#findComment-779507 Share on other sites More sharing options...
richard_PHP Posted March 8, 2009 Author Share Posted March 8, 2009 pasted both amended codes in but it is still displaying the else statement of 'oops'. current code on remove.php <?php $file = $_POST['files']; if (file_exists("$file")){ unlink("$file"); echo "Did it work?"; } else { echo "Oops"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148462-solved-unlink-help/#findComment-779514 Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 The name of your form can not be files, rename it. Then repost everything that you have. Quote Link to comment https://forums.phpfreaks.com/topic/148462-solved-unlink-help/#findComment-779515 Share on other sites More sharing options...
richard_PHP Posted March 8, 2009 Author Share Posted March 8, 2009 renamed the form to 'list'.. still not working files.php <form action="remove.php" method="post" name="list"> <? //define the path as relative $path = "uploads/"; //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); //running the while loop while ($file = readdir($dir_handle)) { if($file!="." && $file!="..") echo "<input name='files' type='radio' value='$file' /><a href='uploads/$file' target='_blank'>$file</a><br />"; } //closing the directory closedir($dir_handle); ?> <p><input name="submit" type="submit" value="Delete File" /></p> </form> remove.php <?php $file = $_POST['list']; if (file_exists("$file")){ unlink("$file"); echo "Did it work?"; } else { echo "Oops"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148462-solved-unlink-help/#findComment-779517 Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 <?php $file = $_POST['files']; if (file_exists("$file")){ unlink("$file"); echo "Did it work?"; } else { echo "Oops"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148462-solved-unlink-help/#findComment-779519 Share on other sites More sharing options...
richard_PHP Posted March 8, 2009 Author Share Posted March 8, 2009 got it working! worked out that in order for the unlink to remove a file, it needs to know the correct path. so adding a variable which declares the containing folder i added a line like so: <?php $file = $_POST['files']; $path = "uploads/"; if (file_exists($path . $file)){ unlink($path . $file); echo "Did it work?"; } else { echo "Oops"; } ?> thanks for everyone's help! ;D Quote Link to comment https://forums.phpfreaks.com/topic/148462-solved-unlink-help/#findComment-779525 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.