Jump to content

Displaying directories


SilverBlade86

Recommended Posts

Hi there, I would like some help with my code. Currently, I have a loop which displays all subfolders in a folder. However, it is also displaying the "." and ".." which are the current root directory and the directory above it. How can I remove it? I do not want it to be displayed.

 

My basic directory structure is the root folder, where all the php files are stored, and a photos folder under it, then a year folder, then an event folder under it. In the event folders will be 2 folders, one for thumbnails and one for the big sized photos. When the user reaches the event part, it will not display folders, but instead will display all thumbnails, which are linked to the big sized photos. I hope that makes sense.

 

This is my code:

 

                                        elseif ($page == "photos") {

                                        	$year = $_GET["year"];
                                        	$event = $_GET["event"];

                                                echo "Which year do you wish to view?<br><br>";

					//Original directory
                                                $path = getcwd() . "\\photos" ;

					//if year is empty
					if (!$year) {
						echo "year is empty";
					}
					else {
						$path = $path . "\\" . $year;
					}

					if (!event) {
						echo "Event empty";
					}
					else {
						$path = $path . "\\" . $event;
					}

                                              	// Open the folder for display
                                                $dir_handle = @opendir($path) or die("Unable to open $path");
                                                // Loop through the files
                                                while (false !== ($file = readdir($dir_handle)))  {
                                                // do not display current file



						//if (is_dir($file)) {
                                                   	

							if (!$year) {
								echo "<a href = \"?page=photos&year=$file\">$file</a> <br>";
							}
							elseif (!$event) {
								echo "<a href = \"?page=photos&year=$year&event=$file\">$file</a> <br>";
							}
                                                  	//}

						if (($year) && ($event)) {

							echo "both event and year are filled <br>";
							echo "<img src=\"$path\\" . $file . "\"> <br>";
						}
                                                }

                                                // Close directory
                                                closedir($dir_handle);

                                        }

Link to comment
Share on other sites

Ugh, I solved that quite easily actually. But I forgot to ask the most important question.

 

When I uncomment is_dir($file), it doesn't work properly! It does not display the subfolders when I run it in a loop. I've done some search on Google, but am unable to solve it. Suggestions would be great.

Link to comment
Share on other sites

<?php

function directory_map($source_dir, $top_level_only = FALSE)
{
        if ($fp = @opendir($source_dir))
        {
                $filedata = array();
                while (FALSE !== ($file = readdir($fp)))
                {
                        if (@is_dir($source_dir.$file) && substr($file, 0, 1) != '.' AND $top_level_only == FALSE)
                        {
                                $temp_array = array();

                                $temp_array = directory_map($source_dir.$file."/");

                                $filedata[$file] = $temp_array;
                        }
                        elseif (substr($file, 0, 1) != ".")
                        {
                                $filedata[] = $file;
                        }
                }
                return $filedata;
        }
}
?>

 

Try that function. Works quite well. It maps out the entire directory and returns it as a multi-dimensional array.

 

ex;

<?php
echo '<pre>';
var_dump(directory_map($_SERVER['DOCUMENT_ROOT']);
echo '</pre>';
?>

 

 

//shamelessly stolen from the CodeIgniter Framework

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.