jhb Posted October 12, 2010 Share Posted October 12, 2010 I have a download-list, and want to have a delete-button/link at each file. I have tried different codes, but i'm new to php and need some help. if ($handle = opendir('files/engelsk/')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $en .= '» <a href="/files/engelsk/'.$file.'">'.$file.'</a> - <i><a href="#">Delete file</a></i><br />'; } } closedir($handle); } It's the "<a href="#">Delete file</a>" I want to be the delete-button. Please help! Quote Link to comment Share on other sites More sharing options...
jhb Posted October 12, 2010 Author Share Posted October 12, 2010 It would also be great if you could help me with not showing the file extension of each file Quote Link to comment Share on other sites More sharing options...
Pawn Posted October 12, 2010 Share Posted October 12, 2010 $dir = "files/engelsk/";if (!$handle = opendir($dir)) { echo "Could not open directory ".$dir; exit;}while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if(isset($_GET['rm'])) { if($_GET['rm'] == $file) { if(unlink($dir.$file)) { // File removed echo $dir.$file." was removed."; } else { // Failed to remove file echo $dir.$file." could not be removed. Check your paths and permissions."; } } } else { $extension = end(explode('.', $file)); $filename_no_ext = substr($file, 0, -(strlen($extension) + 1)); $en .= '» <a href="/files/engelsk/'.$file.'">'.$filename_no_ext.'</a> - <i><a href="?rm='.$file.'">Delete file</a></i><br />'; } }}closedir($handle); Untested. Quote Link to comment Share on other sites More sharing options...
jhb Posted October 12, 2010 Author Share Posted October 12, 2010 Thanks Pawn! Worked great! Only thing that is bad is tha i dont learn a thing by copy-paste Quote Link to comment Share on other sites More sharing options...
Pawn Posted October 12, 2010 Share Posted October 12, 2010 We are passing a variable to the script via the querystring, which is the portion of the url after "?" (see this). Notice that the href has become <a href="?rm='.$file.'">Delete file</a> When that's clicked, we'll go to the same page, but this time setting "rm" to equal the value of $file. Then, when we go through our loop we can check to see if "rm" is set. if(isset($_GET['rm'])) If it is, then we want to know if its value exactly equals the value of the file we are currently iterating over. If it does if($_GET['rm'] == $file) We can unlink it. We check to see if unlink() is TRUE, because we know from checking unlink's return values that that indicates success. if(unlink($dir.$file)) { // File removed echo $dir.$file." was removed."; } else { // Failed to remove file echo $dir.$file." could not be removed. Check your paths and permissions."; } Quote Link to comment Share on other sites More sharing options...
jhb Posted October 12, 2010 Author Share Posted October 12, 2010 wow. Thanks! Quote Link to comment Share on other sites More sharing options...
Pawn Posted October 12, 2010 Share Posted October 12, 2010 The extension bit? $extension = end(explode('.', $file)); explode() is making an array from the string, using a fullstop as the delimiter. So "foo.bar" would become Array("foo", "bar") end() selects the last value of that array, which in your case is the file extension. $arr = array("foo", "bar"); $foo= end($arr); // $foo = bar The last line uses substr() to remove the number of characters in the extension from the end of $file, plus one to accommodate for the "." $filename_no_ext = substr($file, 0, -(strlen($extension) + 1)); Quote Link to comment Share on other sites More sharing options...
jhb Posted October 12, 2010 Author Share Posted October 12, 2010 I got one problem though; when i delete a file I would want a list of the remaining files below the message "example_file.txt was removed." I have tried this, but it doesn't work: <?php $dir = "files/norsk/"; if (!$handle = opendir($dir)) { echo "Kunne ikke åpne mappen ".$dir; exit; } while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if(isset($_GET['rm'])) { if($_GET['rm'] == $file) { if(unlink($dir.$file)) { // File removed $no1 .= $dir.$file.' was removed.'; $extension = end(explode('.', $file)); $filename_no_ext = substr($file, 0, -(strlen($extension) + 1)); $no .= '<img src="document-icon.png" /> '.$filename_no_ext.' <a href="/files/norsk/'.$file.'"><img src="down-icon.png" /></a> <i><a href="?rm='.$file.'" style="color:grey;"><img src="delete-icon.png" /></a></i><br />'; } else { // Failed to remove file $no1 .= $dir.$file.' could not be removed. Check your paths and permissions.'; $extension = end(explode('.', $file)); $filename_no_ext = substr($file, 0, -(strlen($extension) + 1)); $no .= '<img src="document-icon.png" /> '.$filename_no_ext.' <a href="/files/norsk/'.$file.'"><img src="down-icon.png" /></a> <i><a href="?rm='.$file.'" style="color:grey;"><img src="delete-icon.png" /></a></i><br />'; } } } else { $extension = end(explode('.', $file)); $filename_no_ext = substr($file, 0, -(strlen($extension) + 1)); $no .= '<img src="document-icon.png" /> '.$filename_no_ext.' <a href="/files/norsk/'.$file.'"><img src="down-icon.png" /></a> <i><a href="?rm='.$file.'" style="color:grey;"><img src="delete-icon.png" /></a></i><br />'; } } } closedir($handle); ?> <p style="color:grey;vertical-align:middle;"><?=$no1?></p> <p style="color:black;vertical-align:middle;"><?=$no?></p> Quote Link to comment Share on other sites More sharing options...
Pawn Posted October 12, 2010 Share Posted October 12, 2010 Try this: $dir = "files/engelsk/";$list = '';$info = '';if (!$handle = opendir($dir)) { echo "Could not open directory ".$dir; exit;}while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if(isset($_GET['rm']) && $_GET['rm'] == $file) { if(unlink($dir.$file)) { // File removed $info = $dir.$file." was removed."; } else { // Failed to remove file $info = $dir.$file." could not be removed. Check your paths and permissions."; } continue; } $extension = end(explode('.', $file)); $filename_no_ext = substr($file, 0, -(strlen($extension) + 1)); $list .= '» <a href="/files/engelsk/'.$file.'">'.$filename_no_ext.'</a> - <i><a href="?rm='.$file.'">Delete file</a></i><br />'; }}closedir($handle);// Outputif(!empty($info)) { echo "<p>".$info."</p>\n";}if(!empty($list)) { echo $list;} else { echo "The directory is empty.";} Previously, the part that added the list item was held in an else statement that only executed if $_GET['rm'] was not set. We just needed to rearrange the logic a little and add a continue to prevent a file that has just been removed still showing up in the list. Quote Link to comment Share on other sites More sharing options...
jhb Posted October 12, 2010 Author Share Posted October 12, 2010 nice. nice. I just added an javascript alert-box as an temporarily solution, where the OK-button redirects back to the index-file, but i think i will use your code instead Quote Link to comment Share on other sites More sharing options...
jhb Posted October 12, 2010 Author Share Posted October 12, 2010 would there be a way of implementing a way to change the name of file in that code, without showing the file extension? I dont know much of the rename() function so... Quote Link to comment Share on other sites More sharing options...
Pawn Posted October 13, 2010 Share Posted October 13, 2010 Easy enough, but I think you might need to work this one out on your own. The PHP manual tells you everything about rename() that you need to know, and by passing a $_GET variable in the same way as we did with the delete function you'll be able to tell the script which file to rename. Good luck. Quote Link to comment Share on other sites More sharing options...
jhb Posted October 13, 2010 Author Share Posted October 13, 2010 Thanks. I was thinking about making it like the delete-function, so I guess it would be pretty easy after a couple hours of trying different stuff... 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.