Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Getting List Of Directories And Files Using Php


7 replies to this topic

#1 Corey

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 218 posts

Posted 06 March 2005 - 08:42 AM

is there a php function that lists the content of some folder....

example:

/New folder

new.txt
left.gif
download.zip
dc.exe


....so is there..? :D

#2 Ao)K-General

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 523 posts

Posted 06 March 2005 - 12:44 PM

I have no clue. I think there might be but I dunno. There is something where you can register and the info goes to a folder. But I dunno.

#3 yasir

    Newbie [Level 1]

  • Kontributors
  • Pip
  • 12 posts

Posted 06 March 2005 - 01:49 PM

best thing would be to Google it!

Try something like 'Advanced PHP Tutorials'

Cheers!
Yasir :D

#4 HmmZ

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 362 posts
  • Location:The Netherlands

Posted 06 March 2005 - 02:14 PM

I don't think theres a pre-programmed function for that, you could, of course do it manually, updating it manually when your folder has new content:

<?php $folder_content = 'echo "The following files are content of the selected folder:"
echo "$list"';
$list= "new.txt, left.gif, download.zip, dc.exe";

echo "$folder_content"; ?>

You could put that in a new file called "folder_content.php" and then (for example in your indexpage, put the following line:

include("folder_content.php");

That way you can easily edit the content it should display manually, while using minimum bytes for your indexpage.

Hope that works for you (im just a newbie phper :D )

#5 bjrn

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 378 posts

Posted 06 March 2005 - 03:13 PM

Corey, on Mar 6 2005, 09:42 AM, said:

is there a php function that lists the content of some folder....
There is opendir() which you can use to open a dir and then loop through the contents.

<?php
if ($handle = opendir("yourdirhere")) {
  while (($file = readdir($handle)) !== false) {
    echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
  }
  closedir($dh);
}
?> 
Note that this will also list sub-directories and '.' and '..'. You can get info in the php.net documentation for opendir().

#6 mobious

    Advanced Member

  • Kontributors
  • PipPipPipPipPipPipPip
  • 113 posts
  • Location:Philippines

Posted 07 March 2005 - 11:39 AM

i came up with this. it is similar with bjrn's but will sort all folder and files alphabetically. also will be conatined in a multi-dimensional array together with their name and statistics.

$directory = "path/to/dir";

while (($item = $directory->read()) !== false) {
	if ($item != "." && $item != "..") {
  $path = "{$directory->path}/{$item}";
  
  if (is_dir($path)) {
  	$tmp['name'] = $item;
  	$tmp['stats'] = lstat($path);
  	
  	$dirs[$item] = $tmp;
  	
  	unset($tmp);
  } elseif (is_file($path)) {
  	$tmp['name'] = $item;
  	$tmp['stats'] = lstat($path);
  	$tmp['extension'] = substr($item, strrpos($item, "."));
  	
  	$files[] = $tmp;
  	
  	unset($tmp);
  }
	}
}

ksort($dirs, SORT_STRING);
sort($dirs);

ksort($files, SORT_STRING);
sort($files);


#7 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 13 May 2008 - 06:23 AM

Good Php Directory
Getting List Of Directories And Files Using Php

Here is a good PR4 Php directory

Www.Businessservicesuk.Com

-reply by Pete

#8 Guest_nlewis_*

  • Guests

Posted 23 March 2011 - 01:04 AM

Here's a little something I whipped up when Google failed to find anything for me. ;)

function get_dirlist( $path, $match = '*', $exclude = array( '.', '..' ) )
{
  $result = array();

  if( ( $handle = opendir( $path ) ) )
  {
    while( false !== ( $fname = readdir( $handle ) ) )
    {
      $skip = false;

      if( !empty( $exclude ) )
      {
        if( !is_array( $exclude ) )
          $skip = fnmatch( $exclude, $fname );
        else
        {
          foreach( $exclude as $ex )
          {
            if( fnmatch( $ex, $fname ) )
              $skip = true;
          }
        }
      }

      if( !$skip && ( empty( $match ) || fnmatch( $match, $fname ) ) )
        $result[] = $fname;
    }

    closedir( $handle );
  }

  return $result;
}

Simple Usage:

$list = get_dirlist( '.' );

foreach( $list as $fname )
  echo "<p><a href=\"$fname\">$fname</a></p>\n";

Advanced Usage:

$exclude = array
(
  '.',
  '..',
  'index.php'
);

$list = get_dirlist( '.', '*.php', $exclude );

foreach( $list as $fname )
  echo "<p><a href=\"$fname\">$fname</a></p>\n";


#9 Guest_Ankita Gupta_*

  • Guests

Posted 07 October 2011 - 12:45 AM

View PostCorey, on 06 March 2005 - 08:42 AM, said:

is there a php function that lists the content of some folder....

example:

/New folder

new.txt
left.gif
download.zip
dc.exe


....so is there..? :D



Hi,

I have the simplest code for you, just create a php file, having following code


<?php
function getFileList($dir)
  {
    // array to hold return value
    $retval = array();

    // add trailing slash if missing
    if(substr($dir, -1) != "/") $dir .= "/";

    // open pointer to directory and read list of files
    $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
    while(false !== ($entry = $d->read())) {
      // skip hidden files
      if($entry[0] == ".") continue;
      if(!is_dir("$dir$entry") && is_readable("$dir$entry")) {
        $retval[] = "$dir$entry";
      }
    }
    $d->close();

    return $retval;
  }
  // for example your folder name is img for the time being..
  $dirlist = getFileList("img");
  echo "<pre>",print_r($dirlist),"</pre>";
?>
and execute it... And it will give you an array containing the list of all files of that folder, it simple skips the folders and hidden files.
(Note that you have to put this script in the same folder where your img folder exists.... )

Enjoy :)
-Ankita Gupta




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users