...

View Full Version : Folder view works but can the output be formatted?



toxictoad
12-08-2008, 01:03 AM
Hi all,

I've got this script working and it displays the contents of the folder.

I was wondering if someone could show me what I need to add to hide .php files from being displayed?


<?php
$dir = "http://mysite.com/files/uploads/";

// 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);
}
}

if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file<br />";
}
}
closedir($handle);
}
?>

I am really new to php and I'm trying to piece things together so if you could provide and example that would be of a great help.

Thanks
TT

Fou-Lu
12-08-2008, 04:34 AM
There are a number of ways to do this. I'll be using pathinfo with a PHP5+ option identifier:


$aHideExt = array(
'php'
);
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && !in_array(pathinfo($file, PATHINFO_EXTENSION), $aHideExt)) {
echo "$file<br />";
}
}
closedir($handle);
}

Won't work in PHP4, to use pathinfo you'd need to assign it to an array and retrieve the 'extension' index out of it.

Look into wrapping this in a function though. As a function you'll be able to get it to recurse directories and perform a tree listing.

toxictoad
12-08-2008, 10:44 AM
Thank you for the reply Fou-Lu,

I don't really understand what you said :confused:

I will however see what I can do with the code you've provided and if (or when) I get totally lost I'll post back.

I am using php 5 though I know that much :)

TT



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum