Jump to content

Archived

This topic is now archived and is closed to further replies.

Mutley

List folders via PHP?

Recommended Posts

Is it possible so when you create a new folder on your server PHP can detect it and display the name of it?

Share this post


Link to post
Share on other sites
I create a folder using my FTP called "bob". Then on the website, it displays a link to the folder "bob". Like www.mysite.com/bob/ as a hyperlink.

So everytime a new folder is created, it is detected and displays it.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Doesn't seem to work, nothing appears.

At the end I put:
read_contents("/");

I thought that would show all the folders in the directory the file is in?

Share this post


Link to post
Share on other sites
Are you running PHP5 or greater? The function Daniel0 made for you uses scandir() which is only available since PHP5.

Orio.

Share this post


Link to post
Share on other sites
Gah, I've tried a few different methods but can't get that script to work, it just opens a blank page, no source code.

I've tried different directories and all sorts. Yes I'm sure I am on PHP5. Does it work for others?

Share this post


Link to post
Share on other sites
I really need this to be working. If someone can give me a working code and a step by step quote of what each part does, it would mean alot.

Thanks.

Share this post


Link to post
Share on other sites
Try removing the "@" sign before "scandir()".
Also add in the begining of the script error_reporting(E_ALL).
Maybe this will help us to find the source of the problem.

orio.

Share this post


Link to post
Share on other sites
Nope:
Fatal error</b>:  Call to undefined function:  scandir() on line 12

That's with removing the "@" with it left on, it doesn't appear any errors, just blank page.

Share this post


Link to post
Share on other sites
http://ca3.php.net/manual/en/function.scandir.php

scandir is php5 only. The manual reference linked above shows examples of equivalent functionality with php4

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
@ Orio

Where do I add that code or do I use just that code? How do I use the function to chose which directory I'm wanting the folders to be listed from?

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
I think it'll automaticly work if you have the "href" part in quotes (IE <a href="folder name/"> and not <a href=folder name/>) and a slash ("/") in the end...

Orio.

Share this post


Link to post
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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.