...

View Full Version : PHP Array Question



Ryan55
12-01-2010, 10:58 AM
Hey,

I have an array of all the folders in a directory using scandir and i then want to echo out the array but not using print_r. I just want a list of all the folders but the array is going to change depending on the number of folders and i want to be able to echo them all out except the last 2 which are just . and ..

My code is below -


$dir = 'templates';
$files = scandir($dir, 1);

echo "<pre>";
print_r($files);
echo "</pre>";

abduraooft
12-01-2010, 11:23 AM
$files = scandir($dir, 1);
Use
$files=glob($dir."/*"); instead.

Ryan55
12-01-2010, 11:35 AM
Thanks, How can i echo them out outwith the array like

test
default

instead of

Array (
[0] => test
[1] => default
)

But all the array and not specific ones

firepages
12-01-2010, 11:54 AM
foreach($files as $f){
echo $f.'<br />';
}

#or
echo implode('<br />', $files);

Lamped
12-01-2010, 11:56 AM
$files = scandir($folder);
foreach($files as $file) {
if (($file != '.') && ($file != '..')) {
echo($file."\n");
}
}

To display folders only:


$files = scandir($folder);
foreach($files as $file) {
if (($file != '.') && ($file != '..') && is_dir($folder.'/'.$file)) {
echo($file."\n");
}
}

To display files only:


$files = scandir($folder);
foreach($files as $file) {
if (is_file($folder.'/'.$file)) {
echo($file."\n");
}
}

Untested.

abduraooft
12-01-2010, 11:56 AM
echo implode('<br>',$files);

Too late :)

Ryan55
12-01-2010, 12:16 PM
Thanks for your help



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum