Jump to content

Getting a file list with paths


tibberous

Recommended Posts

In a folder? Did you want it to browse subfolders as well?

 

From PHP's website:

<?php
$dir = "/etc/php5/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
        }
        closedir($dh);
    }
}
?>

Link to comment
Share on other sites

You need a recursive function. The concept can be a little abstract at first, but properly calling a function from itself is a great way to get things like this done. Be careful when doing this though, as you can get yourself into an infinite loop quite easily.

 

<?php
// $d is the directory to loop through
// $ind is whatever you want to indent each level of depth, defaut is a few non breaking spaces
// $depth is the ammount of tabs to put before each file in the directory

function makeTree ($d, $ind = "   ", $depth = 0) {

if (!is_dir($d) ) // Check if this isn't a directory
	return FALSE; // If not, return FALSE

if (!$h = opendir($d) ) // Attempt to open a directory handle
	return FALSE; // If not, return FALSE

echo str_repeat($ind, $depth) . '<b>'. $d .'</b><br>' . "\n"; // Echo tabs based on depth, then echo a bold directroy name

while( ($f = readdir($h)) !== FALSE ) { // Loop through all files in current directory
	static $i = 0; // Set increment variable to 0 on first loop only
	if (substr($f, 0, 1) == '.') // Check if first character in file name is '.' (.htaccess, .DS_Store, ect)
		continue; // If it is, skip the rest of the loop and start again on the next file
	if (is_dir($d .'/'. $f)) // Check if this file is actually a folder
		makeTree($d .'/'. $f, $ind, $depth + 1); // If is is a directory, call this function to loop through THAT directory and increase the depth by 1
	else // If it isn't a folder
		echo str_repeat($ind, $depth) . $f . '<br>' . "\n"; // Echo the file name
	$i++; // Increment the variable
}

if ($i < 1) // Check if increment variable is less than one
	echo str_repeat($ind, $depth) . '<small>No files...</small>' . "\n"; // Echo to the user the folder is empty
}
echo '<font face="Courier">';
makeTree('/Applications/MAMP/htdocs');
echo '</font>';
?>

 

And here's a quick modified sample of the function in use

 

<?php

function dirArray ($d, &$a) {

if (!is_dir($d) )
	return FALSE;

if (!$h = opendir($d) )
	return FALSE;

$a = array();
$fa = array();	
while( ($f = readdir($h)) !== FALSE ) {
	if (substr($f, 0, 1) == '.') 
		continue; 
	if (is_dir($d .'/'. $f))
		dirArray($d .'/'. $f, $a[$f . '/']);
	else
		$fa[] = $f;
}
$a = array_merge($a, $fa);

}

function makeTree($a, $g = '', $o = FALSE, $c = '', $depth = 0) {

if (!is_array($o) )
	$o = array();

if (empty($a))
	echo $g . str_repeat('|', $depth) . '-' . '<small>Folder is empty</small><br />' . "\n";

foreach ($a as $dir => $file) {

	echo $g . str_repeat('|', $depth) . '-';
	if (is_array($file) ) {

		if (in_array($c . $dir, $o) ) {
			echo '<b><a href="?c='.$c.$dir.'">(-)</a> <a href="?open='.$c.$dir.'">'.substr($dir, 0, -1).'</a></b><br />' . "\n";
			makeTree($file, $g, $o, $c.$dir, $depth+1);
		} else
			echo '<b><a href="?e='.$c.$dir.'">(+)</a> <a href="?open='.$c.$dir.'">'.substr($dir, 0, -1).'</a></b><br />' . "\n";

	} else
		echo $file . '<br />' . "\n";

}



}

session_start();

if (!is_array($_SESSION['open_dirs']) )
$_SESSION['open_dirs'] = array();

if ($_GET['open']) {
$_SESSION['current_dir'] = $_GET['open'];
if (!in_array($_GET['open'], $_SESSION['open_dirs']) )
	$_SESSION['open_dirs'][] = $_GET['open'];
}
if ($_GET['c'])
if ( ($key = array_search($_GET['c'], $_SESSION['open_dirs'])) !== FALSE )
	unset($_SESSION['open_dirs'][$key]);
if ($_GET['e'])
if (!in_array($_GET['e'], $_SESSION['open_dirs']) )
	$_SESSION['open_dirs'][] = $_GET['e'];

dirArray('/Applications/MAMP/htdocs', $return);
echo '<font face="Courier">' . "\n";
makeTree($return, ' ', $_SESSION['open_dirs']);
echo '</font>';



?>

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.