Jump to content

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);
    }
}
?>

thanks!

 

this is probably a dumb question but.... how do I get rid of the links to the directory and only link to the files within it?

 

also, when I upload a file with space in its name, the link in the list won't work. how do I stop that happening?

 

 

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

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.