cixdk Posted June 1, 2008 Share Posted June 1, 2008 Spent a good 3-4 hours playing around still can't get this to work properly. I'm fairly new to php in general. Purpose of code: Read files in a directory and display image icon, name, and a checkbox. The check box is to delete the images. It works fine except it doesn't delete any selected files, or give me an error. Any help would be.. well helpful <?php $path = "/home/cix/public_html/uploads"; $dir_handle = @opendir($path) or die("Unable to open $path"); echo "Files in this Directory: <form enctype='multipart/form-data' action='list.php' method=POST><br/>"; while ($file = readdir($dir_handle)) { if($file!="." && $file!="..") echo " <img src='http://cixdev.info/uploads/$file' height='50' width='50' alt='$file'/> <a href='http://cixdev.info/uploads/$file'>http://cixdev.info/uploads/$file</a> <input type=checkbox name=$file><br/> "; } echo "<input type=submit name='GO'></form>"; closedir($dir_handle); if(isset($_POST[$file])) { $checkbox = $_POST[$file]; if($checkbox == on) { if(!unlink($file)) die("Couldn't Delete file"); } } ?> Link to comment https://forums.phpfreaks.com/topic/108229-direct-listing-wont-delete-files/ Share on other sites More sharing options...
joquius Posted June 1, 2008 Share Posted June 1, 2008 What would be even more helpful is the error text. Do you have sufficient permissions for the directory/files? Link to comment https://forums.phpfreaks.com/topic/108229-direct-listing-wont-delete-files/#findComment-554765 Share on other sites More sharing options...
cixdk Posted June 1, 2008 Author Share Posted June 1, 2008 What would be even more helpful is the error text. Do you have sufficient permissions for the directory/files? Maybe I worded it wrong, I do not get any errors. Link to comment https://forums.phpfreaks.com/topic/108229-direct-listing-wont-delete-files/#findComment-554771 Share on other sites More sharing options...
joquius Posted June 1, 2008 Share Posted June 1, 2008 What's this: $_POST[$file]; Where is $file set? There's no point in sending a form when you don't know what the names of the fields are going to be. The values should be variable not the field names. Link to comment https://forums.phpfreaks.com/topic/108229-direct-listing-wont-delete-files/#findComment-554775 Share on other sites More sharing options...
cixdk Posted June 1, 2008 Author Share Posted June 1, 2008 What's this: $_POST[$file]; Where is $file set? There's no point in sending a form when you don't know what the names of the fields are going to be. The values should be variable not the field names. $file is set by the name of the file, so for the check boxes name=$file; make the name of checkbox the same name as the file? so if a check box was checked named corresponding to a file you wish to delete, you would be passing the name of the file to delete $_POST[$file] I am probably completely wrong though. Link to comment https://forums.phpfreaks.com/topic/108229-direct-listing-wont-delete-files/#findComment-554784 Share on other sites More sharing options...
haku Posted June 1, 2008 Share Posted June 1, 2008 $dir_handle = @opendir($path) or die("Unable to open $path"); This doesn't make sense. By using the '@' sign, you are telling it to skip over this function if there is an error, but then you are telling it to die if there is an error. You need to pick one - I would go with 'die'. The '@' sign is bad programming, as it allows you to skip over errors rather than fixing them. You really should pretty much never use it. <form enctype='multipart/form-data' action='list.php' method=POST> You need to enclose the method in quotes. <input type=checkbox name=$file> The type and name need to be enclosed in quotes. <input type=submit name='GO'> The type needs to be enclosed in quotes. if(isset($_POST[$file])) The only way this would work is if you enclose the 'if' in a while statement while reading through the directory again, as you did when echoing out the images in the first place. You will have to rethink this statement. Link to comment https://forums.phpfreaks.com/topic/108229-direct-listing-wont-delete-files/#findComment-554816 Share on other sites More sharing options...
cixdk Posted June 2, 2008 Author Share Posted June 2, 2008 So I re-worked it, and decided using $_get and a simple link would be a whole lot easier. Still accomplishes what I wanted too! <?php $file = $_GET['file']; $upload_dir = './uploads/'; $file_path = $upload_dir . $file; if(is_file($file_path)) { unlink($file_path); echo "File " . $file . " deleted."; } $path = "./uploads/"; $dir_handle = opendir($path) or die("Unable to open $path"); echo "Files in this Directory: <br/>"; while ($file = readdir($dir_handle)) { if($file!="." && $file!="..") { echo "<img src='http://cixdev.info/uploads/$file' height='50' width='50' alt='$file'/> <a href='http://cixdev.info/uploads/$file'>http://cixdev.info/uploads/$file</a> <a href='http://cixdev.info/list.php?file=$file'>Delete</a><br> "; } } closedir($dir_handle); ?> Link to comment https://forums.phpfreaks.com/topic/108229-direct-listing-wont-delete-files/#findComment-555878 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.