Showing Directories

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tinatina
Forum Newbie
Posts: 10
Joined: Thu Aug 30, 2007 12:06 am

Showing Directories

Post by tinatina »

Hi
my friend wanted me to make a script that lists files in a directory and has the directories on top. I haven't looked for a script from anyone else to do this, I'd like to start from scratch and get it working on my own. Here is some stuff i made from a script that i found 4 years ago.

Code: Select all

<?php
$path = "/home/peacher/public_html/directory/";
// Open the folder

$dir_handle = @opendir($path) or die('Unable to open $path');
// Loop through the files

/////this will list directories
while ($file = readdir($dir_handle)) 
	{
		if($file == "." )//what files not to display

		continue;
	
		//if the file is a folder
		if((mime_content_type($file)) === 'httpd/unix-directory')
		{

				$files[] = $file;////puts the files into an array and sorts them
				sort($files);

			foreach ($files as $a) ///prints just the name
			{
				echo $a;
				print(' ' . mime_content_type($a));
				print('<br>');
			}
			print('<br>');
		}//endif
	}
// Close
closedir($dir_handle);
?>
The output so far is like this

a httpd/unix-directory

a httpd/unix-directory
z httpd/unix-directory

.. httpd/unix-directory
a httpd/unix-directory
z httpd/unix-directory

.. httpd/unix-directory
a httpd/unix-directory
test httpd/unix-directory
z httpd/unix-directory

I think the repeating has something to do with the while statement? My heads all clouded now so could someone help me a little to get it to just print that very last result? Thanks :)
purefan
Forum Newbie
Posts: 15
Joined: Wed Aug 01, 2007 9:35 am

Post by purefan »

try this instead:

Code: Select all

while (false !== ($file = readdir($dir_handle))) {
josa
Forum Commoner
Posts: 75
Joined: Mon Jun 24, 2002 4:58 am
Location: Sweden

Post by josa »

I took the liberty to cut and paste your code around a little. It's untested though...

Code: Select all

<?php
$path = "/home/peacher/public_html/directory/";
// Open the folder

$dir_handle = @opendir($path) or die('Unable to open $path');
// Loop through the files

/////this will list directories
while ($file = readdir($dir_handle))
        {
                if($file == "." )//what files not to display

                continue;
       
                //if the file is a folder
                if((mime_content_type($file)) === 'httpd/unix-directory')
                {

                                $folders[] = $file;////puts the folders into an array
                }
                //...or else it's a file
                else
                {
                                $files[] = $file;////puts the files into an array
                }
        }
// Close
closedir($dir_handle);
sort($folders);
sort($files);
foreach ($folders as $a) ///prints just the name
{
        echo $a;
        print(' ' . mime_content_type($a));
        print('<br>');
}
foreach ($files as $a) ///prints just the name
{
        echo $a;
        print(' ' . mime_content_type($a));
        print('<br>');
}
print('<br>');

?>
/josa
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

I'd replace

Code: Select all

(mime_content_type($file)) === 'httpd/unix-directory'
with

Code: Select all

is_dir($file)
tinatina
Forum Newbie
Posts: 10
Joined: Thu Aug 30, 2007 12:06 am

Post by tinatina »

thanks so much :)
Post Reply