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
https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-93050
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
https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-94020
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
https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-94027
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
https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-94045
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
https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-94577
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
https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-97590
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
https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-97664
Share on other sites

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.