...

View Full Version : Php Help-Getting files



Kura_kai
06-10-2005, 03:55 AM
what commands will return all the files of a directory in a array. Or something along that line.

CrAzY_J
06-10-2005, 04:49 AM
<?php
$save_in_array=file('my_file.dat');
?>

Kura_kai
06-10-2005, 11:04 PM
No not a file but all files in a folder

Brandoe85
06-11-2005, 12:44 AM
<?php

function arrFiles ($dir)
{
$handle = opendir($dir);

while ($file = readdir($handle))
{
if ($file != '.' && $file != '..')
{
$fileArr[] = $file;
}
}

closedir($handle);
return $fileArr;
}

$f = arrFiles("C:\\");

foreach($f as $allFiles)
{
echo $allFiles . "<br>";
}

?>


Could be better...

delinear
06-11-2005, 04:13 AM
Just a couple of things with that. Firstly, I don't know how it works on IIS, but on a *nix machine directories are files, too. Therefore the function returns not only files in the directory but sub directories too (which is fine if that's what you want, but just be aware that it's happening). Also, due to the way while loops work you will always receive one empty array on the end which serves no purpose.

Here's a slight re-working which strips out all directories and empty arrays and leaves you just the filenames:


function get_filenames($dir='.') {
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
$check_file = (!empty($file) && !is_dir($file) ? $file : '');
if(!empty($check_file)) {
$return_file[] = $check_file;
}
}
closedir($handle);
return $return_file;
}
}

print_r(get_filenames());



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum