Jump to content

Subdirectories with links


dmirsch

Recommended Posts

In the MySQL help forum someone asked how to list out all the files in a directory. Another person gave the following code:

<?php

$handle = opendir('PUT NAME OF YOUR DIR HERE');
while (false !== ($file = readdir($handle)))
{
    if ($file != "." && $file != "..")
{
        echo $file,'<br />';
    }
}
closedir($handle);
?>

 

I found this to be very helpful! However, is there a way to modify it so that if there are subdirectories, it will list those as hyperlinks so the code would run off of the subdirectory if you were to click that link?

 

When I asked that question in the MySQL forum, I was told that it had nothing to do with PHP which I know. So that's why I am posting it here. Can anyone help?

Link to comment
Share on other sites

$path = '/path/to/your/folder/';

$handle = opendir($path);

while (false !== ($file = readdir($handle)))

{

    if ($file != "." && $file != "..")

    {

        if( is_dir($path.$file) )

        echo '<a href="/'.$file.'">'.$file.'</a>';

        else

        echo $file;

        echo '<br>';

    }

}

closedir($handle);

Link to comment
Share on other sites

So I used xyph's code and it is not reading anything as a directory. Here's how I modified the code:

<?php

$path = '../../images';
$handle = opendir($path);
while (false !== ($file = readdir($handle)))
{
    if ($file != "." && $file != "..")
    {
        if( is_dir($path.$file) )
           echo '<a href="'.$path.$file.'">'.$file.'</a> is a directory';
        else
           echo $file.' is not a directory';
        echo '<br>';
    }
}
closedir($handle);
?>

 

The sample page is here: http://acpacenterstage.com/centerstage/centertix-managers/reading-files-in-folder.php

As you can see, nothing is reading as a directory even though all but the last two are directories.

 

Any ideas?

Link to comment
Share on other sites

Close, I had to add a forward slash to make it really go to the directory.

 echo '<a href="'.$path.$file.'/">'.$file.'</a> is a directory';

 

However, I would like the code to rerun with that new directory once the hyperlink is clicked, because now it tells me,

Access Forbidden

Access denied. Please click on the back button to return to the former page.

since there is no real file name listed in the hyperlink.

 

So somehow if I'd like the coding to keep cycling through until it see no more subdirectories. Is that possible?

Link to comment
Share on other sites

From my library.

 

Make sure you don't close the directory with a slash.

 

ex.

full/path/to/directory/ <-wrong
full/path/to/directory <-right

 

<?php
function dirContents($dir) {
if (is_dir($dir)) {	
	if ($dh = opendir($dir)) {			
		while (($file = readdir($dh)) !== false) {
			if(in_array($file,array('.','..'))) { continue; }
			if(is_dir($dir.'/'.$file)) {					
				$contents[$dir.'/'.$file] = dirContents($dir.'/'.$file);
			} else {
				$contents[] = $dir . '/' . $file;
			}
		}			
		closedir($dh);
	}
}
return $contents;
}

function processArray($array) {
if(is_array($array)) {
	echo '<ol>';
	foreach($array as $key => $value) {
		if(is_int($key)) {
			echo '<li><a href="'.$value.'">'.str_replace('/','',strstr(strrchr($value,'/'),'.',true)).'</a></li>';
		}
		else {
			echo '<li><span style="font-weight:bold">' . $key . '</span>';
			processArray($value);
			echo '</li>';
		}
	}
	echo '</ol>';
}
}

processArray(dirContents('PATH TO DIRECTORY'));
?>

Link to comment
Share on other sites

This is almost there. It gives me the following error though:

Warning: Wrong parameter count for strstr() in /data/23/2/100/53/2263053/user/2487001/htdocs/centerstage/centertix-managers/reading-files-in-folder.php on line 42

Here's the code that goes on line 42:

echo '<li><a href="'.$value.'">'.str_replace('/','',strstr(strrchr($value,'/'),'.',true)).'</a></li>';

Link to comment
Share on other sites

Thanks jcbones! This works. Last thing though, there are a bunch of .LCK files on the server that I do not want to show up in the list. If there an easy way to edit the coding to make those a no-show?

 

Here's the code once again:

<?php
function dirContents($dir) {
if (is_dir($dir)) {	
	if ($dh = opendir($dir)) {			
		while (($file = readdir($dh)) !== false) {
			if(in_array($file,array('.','..'))) { continue; }
			if(is_dir($dir.'/'.$file)) {					
				$contents[$dir.'/'.$file] = dirContents($dir.'/'.$file);
			} else {
				$contents[] = $dir . '/' . $file;
			}
		}			
		closedir($dh);
	}
}
return $contents;
}

function processArray($array) {
if(is_array($array)) {
	echo '<ol>';
	foreach($array as $key => $value) {
		if(is_int($key)) {
			$name = strrchr($value,'/');
			echo '<li><a href="'.$value.'">'.substr($name,1).'</a></li>';
		}
		else {
			echo '<li><span style="font-weight:bold">' . $key . '</span>';
			processArray($value);
			echo '</li>';
		}
	}
	echo '</ol>';
}
}

processArray(dirContents('../../images'));
?>

Link to comment
Share on other sites

  • 5 years later...
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.