Jump to content

list files in a directory


HalRau

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/251613-list-files-in-a-directory/
Share on other sites

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?

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/>";
}
?>

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/>";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.