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
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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.