I have a directory with two files, i whant to store the file names into array,bath when i open the directory and tray to store the file names i am geting two arrays,one [0] => may.log and second [0] => may.log, [1] => april.log. Hier is my code:
<?php
$dir = 'data';
$fileNames = array();
if(is_dir($dir)){
$handle = opendir($dir);
while(false !== ($file = readdir($handle))){
if(is_file($dir.'/'.$file) && is_readable($dir.'/'.$file)){
$fileNames[] = $file;
$fileNames = array_reverse($fileNames);
print_r($fileNames);
}
}
closedir($handle);
}else {
echo "<p>There is an directory read issue</p>";
}
?>
$files = glob("data/*.*");And you all have it in a array! (See: php.net/manual/en/function.glob.php)glob("data/*")? File names don't have to contain an extension and even if they do,*should still match the whole name, right?*will also match folders. Valid files (in windows) have an extension. So,*.*would theoretically only return files, rather than files and folders. (assuming all the files HAVE an extension, which they mostlikely do have if they are served for download.) Even if the server is unix, I never saw anybody remoing extensions like.pdf, .zipjust because they are not required.*and check if its a file or folder when processing elements. (is_file()andis_dir())