You are here: Re: readdir question « All PHP « IT news, forums, messages
Re: readdir question

Posted by DrTebi on 10/13/26 11:26

On Mon, 12 Sep 2005 18:21:24 +0000, Brian wrote:

> Hi all
>
> Below is a bit of code that puts the file list of a dir
> into an array which I use later.
>
> 2 questions.......
>
> 1) how do I get the loop to only show files and not other directories?
>

Simply use is_dir:

$dir_handle = opendir('.') or die($php_errormsg);
while (false !== ($file = readdir($dir_handle))) {
if ($file != '.' && $file != '..' && !is_dir($file)) {
$files[] = $file;
}
} sort($files);
closedir($dir_handle);

Just remember if your initial directory is not '.', but e.g. 'files', you
would have to add the path to the is_dir check. Best to do this with a
constant:

define('INITIAL_DIR', 'files');
$dir_handle = opendir(INITIAL_DIR) or die($php_errormsg);
while (false !== ($file = readdir($dir_handle))) {
if ($file != '.' && $file != '..' && !is_dir(INITIAL_DIR . '/' . $file)) {
$files[] = $file;
}
} sort($files);
closedir($dir_handle);

Cheers,
DrTebi


> 2) Quite sure this can't be done other wise hacking would be so easy,
> but is it possible to read the dir on a different server, and get the file
> list
> in date order of creation?
>
> Thanks
>
> Brian
>
>
>
>
> if ($handle = opendir($path)) {
> $thefiles = array();
> while (false !== ($file = readdir($handle))) {
> $ext = strrchr( $file, '.' );
> if ( $ext == '.zip' || $ext == '.csv' || $ext == '.txt' || $ext ==
> '.doc' || $ext == '.jpg' || $ext == '.bmp'
> || $ext == '.gif' || $ext == '.php' || $ext == '.htm' || $ext ==
> '.html' || $file !== 'fileviewer.php') {
> $thefiles[] = $file;
> }
> }
> closedir($handle);
> rsort($thefiles);
> reset($thefiles);
> }

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация