Jump to content

List folders via PHP?


Mutley

Recommended Posts

Here is an example: [code]<?php
header("Content-type: text/plain");

function read_contents($directory='.')
{
if(substr($directory,-1) != '/')
{
$directory .= "/";
}

$contents = @scandir($directory);

if(is_array($contents))
{
foreach($contents as $item)
{
if($item != '.' && $item != '..')
{
if(is_dir($directory.$item))
{
echo "{$directory}{$item}\n";
}
}
}
}
}

read_contents("/home/daniel");
?>
[/code]
Link to comment
Share on other sites

So you dont have scandir() => You have PHP<5...
Add this to your code, and it should work:

[code]<?php

if(!function_exists('scandir')) {
  function scandir($dir, $sortorder = 0) {
      if(is_dir($dir))        {
          $dirlist = opendir($dir);
          while( ($file = readdir($dirlist)) !== false) {
              if(!is_dir($file)) {
                  $files[] = $file;
              }
          }
          ($sortorder == 0) ? asort($files) : rsort($files); // arsort was replaced with rsort
          return $files;
      } else {
      return FALSE;
      break;
      }
  }
}

?>[/code]

** Taken from [url=http://www.php.net/manual/en/function.scandir.php]scandir()[/url], user's notes.

Orio.
Link to comment
Share on other sites

The code should look like:

[code]<?php

if(!function_exists('scandir')) {
   function scandir($dir, $sortorder = 0) {
       if(is_dir($dir))        {
           $dirlist = opendir($dir);
           while( ($file = readdir($dirlist)) !== false) {
               if(!is_dir($file)) {
                   $files[] = $file;
               }
           }
           ($sortorder == 0) ? asort($files) : rsort($files);
           return $files;
       } else {
       return FALSE;
       break;
       }
   }
}

function read_contents($directory='.')
{
if(substr($directory,-1) != '/')
{
$directory .= "/";
}

$contents = @scandir($directory);

if(is_array($contents))
{
foreach($contents as $item)
{
if($item != '.' && $item != '..')
{
if(is_dir($directory.$item))
{
echo "{$directory}{$item}\n";
}
}
}
}
}

read_contents($some_dir);

?>[/code]

Orio.
Link to comment
Share on other sites

one more question: are you looking for the function to be recursive? i mean, if you have a new folder created, and you create folders within that folder, are you wanting to display them all on the page? if so, try something like this:
[code]
<?php
function getFileStructure($dir = './') {
  $out = "<ul>\n";
  if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
      while (($file = readdir($dh)) !== false) {
        if ($file != '.' && $file != '..') {
          if (is_dir($dir . $file)) {
            $out .= "<li>{$dir}{$file}</li>\n";
            $out .= getFileStructure($dir . $file . '/');
          }
        }
      }
    }
    closedir($dh);
  }
  $out .= "</ul>\n";
  if ($out == "<ul>\n</ul>\n") $out = '';
  return $out;
}
?>
[/code]
Link to comment
Share on other sites

If I have a folder called "Yes and no" for example, with spaces, how do I make it display as "Yes and no" in the list but work as a link when you click it? It can't find a folder called "Yes and no" because it should have %20 in I think.

Maybe something like "Yes-and-no" but the list filters out the - so it looks like "Yes and no" but the link still stays as "Yes-and-no". If you understand me?
Link to comment
Share on other sites

The problem is Orio, I do it like this using varialbes, I already have quotes:

[code]if(is_dir($directory.$item))
{
$team = $_GET['team'];
?>
<p><a href="teamsgallery3.php?team=<?=$team?>&subject=2006&game=<?=$item?>"><?=$item?></a> - <? include("gallery/images/".$team."/2006/".$item."/date/date.txt");?></p><?
}
}
}
}
}

read_contents("gallery/images/".$team."/2006");[/code]
Link to comment
Share on other sites

Do something like this:
[code]<?php
$folder_name = "Yes and no";
echo "<a href='file.php?folder=".urlencode($folder_name)."'>{$folder_name}</a>";
?>[/code]

That will output: [code]<a href='file.php?folder=Yes+and+no'>Yes and no</a>[/code]

Then you can just urldecode it on the other page.
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.