Jump to content


Directory Listings


  • Please log in to reply
7 replies to this topic

#1 Guest_MikeFairbrother_*

Guest_MikeFairbrother_*
  • Guests

Posted 26 June 2005 - 07:30 PM

I have added a new snippet to the Snippet Library!

Snippet name: Directory Listings
Author: MikeFairbrother
Version: 1.0 (Beta)
More info: View this snippet
Description:
This is only BETA - I need a little help.
This snippet will list all files/folders in the current directory of the page you are on. Can be helpful for "image" folders etc.

Please feel free to comment on this snippet, suggest improvements, or simply praise my work! ;)

#2 mrruben5

mrruben5

    Likes Etomite Forums!

  • Member
  • PipPip
  • 255 posts

Posted 27 June 2005 - 06:57 PM

You need to return the output, not echo it. Then it'll work fine.

#3 Guest_MikeFairbrother_*

Guest_MikeFairbrother_*
  • Guests

Posted 28 June 2005 - 11:21 AM

Thanks mrruben5, i've credited you in the snippet.

#4 Guest_MikeFairbrother_*

Guest_MikeFairbrother_*
  • Guests

Posted 28 June 2005 - 11:26 AM

I have updated this snippet in the Snippet Library!

[This is an automatically created message]

#5 Byggemanden

Byggemanden

    Etomite Forum Newbie

  • Member
  • 4 posts

Posted 12 March 2007 - 07:34 AM

I have updated this snippet in the Snippet Library!

[This is an automatically created message]


Hi

How do I use the snippet??? [[directory???????????

#6 Ralph

Ralph

    Loves Etomite Forums!

  • Admin
  • 6,539 posts

Posted 12 March 2007 - 01:28 PM

Hi

How do I use the snippet??? [[directory???????????

Try using the code below... It should be self-explanatory...

// Directory - Snippet
// Usage: [ !Directory?dir=rel_dir_path! ]
// if $dir is not sent in the snippet call, show current dir
$dir = isset($dir) ? $dir : ".";
if ($handle = opendir($dir)) { 
	while (false !== ($entry = readdir($handle))) { 
		if ($entry != "." && $entry != "..") { 
			return "$entry\n"; 
		} 
	} 
	closedir($handle); 
}


#7 PaulD

PaulD

    Likes Etomite Forums!

  • Developers
  • PipPip
  • 413 posts

Posted 12 March 2007 - 06:30 PM

Cool piece of code: Have I understood it correctly (I realise this is not a 'learn PHP forum' :rolleyes: )

The line while (false !== ($entry = readdir($handle))) is astounding!

This is my interpretation of the snippet:

$dir = isset($dir) ? $dir : "."; If directory is set, fine, if not set it to '.' which means current directory
if ($handle = opendir('.')) { If the uninitialised variable $handle is = to current directory then:
while (false !== ($entry = readdir($handle))) { repeat while there is something to read in the directory
if ($entry != "." && $entry != "..") { if the directory reading is not . or ..
return "$entry\n"; send the entry
}
}
closedir($handle); close the directory opened earlier
}

Is this right?

I thought you had to write output to a variable first then output that variable with the return command, but it seems you can write output on the fly, so to speak.

Very interesting piece of code (for a humble amateur like myself).

Thanks for the snippet.

Paul

#8 Ralph

Ralph

    Loves Etomite Forums!

  • Admin
  • 6,539 posts

Posted 12 March 2007 - 07:25 PM

PaulD, your assumptions are correct... If you revisit my posted code you will find that I made a change to it, however... Notice that I have changed the code to read opendir($dir) instead of the original opendir('.'), which resolves an oversight on my part... The results, if the process finds valid results, are returned in $entry as opposed to the standard return variable, $output...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users