Jump to content

how to list files on server?


zipadee

Recommended Posts

heres a simple example from the manual opendir

<?php
$dir = "/etc/php5/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
        }
        closedir($dh);
    }
}
?>

try this (untested)

(my boss is near ;))

<?php
$dir = "/etc/php5/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false)
        {
            if($file != "." || $file != "..")
            {
                 $file = urlencode($file);
                 echo "filename: $file : <br>\n";
            }
        }
        closedir($dh);
    }
}
?>

erm....it doesn't seem to work. but that could just be the way i'm implementing it :S

 

i found this code in a tutorial:

 

<?php 

$folder = "articles/";
$handle = opendir($folder);

# Making an array containing the files in the current directory:
while ($file = readdir($handle))
{
    $files[] = $file;
}
closedir($handle);

#echo the files
foreach ($files as $file) {
    echo "<a href=$folder$file>$file</a>"."<br />";
}
?>

 

is that a legitimate way of doing it too?

 

sorry, i'm not very good at this :(

try this

 

<?php 

$folder = "articles/";

// Open a known directory, and proceed to read its contents
if (is_dir($folder )) {
    if ($dh = opendir($folder)) {
        while (($file = readdir($dh)) !== false)
        {
            if($file != "." || $file != "..")
            {
                 $ufile = urlencode($file);
                 echo "<a href='".$folder."/".$ufile."'>$file</a>"."<br />";
            }
        }
        closedir($dh);
    }
}
?>

 

You could use scandir().

 

*Not Tested

<?php
$folder = "something/"
$dir = $_SERVER['DOCUMENT_ROOT']."/".$folder;
$files = scandir($dir);
foreach($files as $number => $filename) {

   if(filetype($filename) == "dir") {
   
   }
   else {
   echo "<a href='".$dir.$filename."'>$filename</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.