Jump to content


Photo

List folders via PHP?


  • Please log in to reply
20 replies to this topic

#1 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 16 September 2006 - 08:41 AM

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

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 16 September 2006 - 10:21 AM

What do you mean?
Can you give an example maybe?

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 16 September 2006 - 02:45 PM

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.
~ Mutley.

#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 16 September 2006 - 03:42 PM

Here is an example:
<?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");
?>


#5 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 17 September 2006 - 03:33 PM

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?
~ Mutley.

#6 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 17 September 2006 - 06:35 PM

Are you running PHP5 or greater? The function Daniel0 made for you uses scandir() which is only available since PHP5.

Orio.
Think you're smarty?

(Gone until 20 to November)

#7 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 18 September 2006 - 07:10 AM

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?
~ Mutley.

#8 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 18 September 2006 - 03:21 PM

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.
~ Mutley.

#9 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 18 September 2006 - 03:25 PM

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.
Think you're smarty?

(Gone until 20 to November)

#10 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 18 September 2006 - 03:38 PM

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.
~ Mutley.

#11 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 18 September 2006 - 03:42 PM

http://ca3.php.net/m...ion.scandir.php

scandir is php5 only. The manual reference linked above shows examples of equivalent functionality with php4
Legend has it that reading the manual never killed anyone.
My site

#12 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 18 September 2006 - 03:43 PM

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

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

?>

** Taken from scandir(), user's notes.

Orio.
Think you're smarty?

(Gone until 20 to November)

#13 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 18 September 2006 - 03:46 PM

@ 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?
~ Mutley.

#14 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 18 September 2006 - 03:54 PM

The code should look like:

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

?>

Orio.
Think you're smarty?

(Gone until 20 to November)

#15 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 18 September 2006 - 04:34 PM

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

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#16 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 18 September 2006 - 04:35 PM

Thanks alot they work. :)

Finally, this has been bugging me all week!
~ Mutley.

#17 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 19 September 2006 - 12:32 PM

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?
~ Mutley.

#18 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 19 September 2006 - 12:53 PM

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.
Think you're smarty?

(Gone until 20 to November)

#19 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 24 September 2006 - 10:14 AM

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

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");

~ Mutley.

#20 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 24 September 2006 - 05:14 PM

Can anyone see how I fix this problem?
~ Mutley.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users