HalRau Posted November 22, 2011 Share Posted November 22, 2011 I am trying to find a script that will show just the file names of files in a specific directory. I tried this code posted previously on this forum and it works but shows the directory name. <?php filesInDir('attachments'); function filesInDir($tdir) { $dirs = scandir($tdir); foreach($dirs as $file) { if (($file == '.')||($file == '..')) { } elseif (is_dir($tdir.'/'.$file)) { filesInDir($tdir.'/'.$file); } else { echo $tdir.'/'.$file."<br>"; } } } ?> Result is: attachments/Sedona_and_Slot_Canyon_links.docx Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/251613-list-files-in-a-directory/ Share on other sites More sharing options...
ZulfadlyAshBurn Posted November 22, 2011 Share Posted November 22, 2011 try implementing this to your code<?php $dir = "tt/"; foreach (glob($dir . "*.*") as $filename) { $filename = str_replace($dir, "", $filename); echo "$filename size <br/>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/251613-list-files-in-a-directory/#findComment-1290412 Share on other sites More sharing options...
HalRau Posted November 22, 2011 Author Share Posted November 22, 2011 Thanks ZulfadlyAshBurn It works great except for the "size" tag which isn't all that necessary for this application. Do you have any suggestions on adding a "delete" function to your code that would delete a file from the attachments folder via a link or button? Quote Link to comment https://forums.phpfreaks.com/topic/251613-list-files-in-a-directory/#findComment-1290427 Share on other sites More sharing options...
ZulfadlyAshBurn Posted November 22, 2011 Share Posted November 22, 2011 opps, i copied the script from some of my script. sorry... <?php $dir = "tt/"; if(!empty($_GET['delete'])) { $filename = $dir . $_GET['delete']; $name = $_GET['delete']; if (file_exists($filename)) { if (unlink($filename)) { echo "File '" .$name."' deleted!"; } else { echo "File '" .$name."' cannot be deleted!"; } } else { echo "File '" .$name."' not found!"; } echo "<hr/>"; } foreach (glob($dir . "*.*") as $filename) { $filename = str_replace($dir, "", $filename); echo $filename . " | <a href='?delete=".$filename."'>delete</a> <br/>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/251613-list-files-in-a-directory/#findComment-1290431 Share on other sites More sharing options...
HalRau Posted November 22, 2011 Author Share Posted November 22, 2011 Thanks again ZulfadlyAshBurn! The script works great. I would like to not have it show "File name not found!" and the "hr" after deleting a file. I have tried commenting these out but haven't been successful. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/251613-list-files-in-a-directory/#findComment-1290493 Share on other sites More sharing options...
ZulfadlyAshBurn Posted November 23, 2011 Share Posted November 23, 2011 no problem here you go : <?php $dir = "tt/"; if(!empty($_GET['delete'])) { $filename = $dir . $_GET['delete']; $name = $_GET['delete']; if (file_exists($filename)) { if (unlink($filename)) { echo "File '" .$name."' deleted!<br/>"; } } } foreach (glob($dir . "*.*") as $filename) { $filename = str_replace($dir, "", $filename); echo $filename . " | <a href='?delete=".$filename."'>delete</a> <br/>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/251613-list-files-in-a-directory/#findComment-1290708 Share on other sites More sharing options...
HalRau Posted November 23, 2011 Author Share Posted November 23, 2011 Thank you ZulfadlyAshBurn. Works perfect. Really appreciate you helping this php newbie out. Quote Link to comment https://forums.phpfreaks.com/topic/251613-list-files-in-a-directory/#findComment-1290715 Share on other sites More sharing options...
ZulfadlyAshBurn Posted November 23, 2011 Share Posted November 23, 2011 HAHA. no problem Quote Link to comment https://forums.phpfreaks.com/topic/251613-list-files-in-a-directory/#findComment-1290720 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.