note 75118 added to function.opendir

From: Date: Sun, 13 May 2007 13:00:26 +0000
Subject: note 75118 added to function.opendir
Groups: php.notes 
Request: Send a blank email to php-notes+get-126477@lists.php.net to get a copy of this message
I sometimes find this useful. Hope you will too. <?php //list_by_ext: returns an array containing an alphabetic list of files in the specified directory ($path) with a file extension that matches $extension function list_by_ext($extension, $path){ $list = array(); //initialise a variable $dir_handle = @opendir($path) or die("Unable to open $path"); //attempt to open path while($file = readdir($dir_handle)){ //loop through all the files in the path if($file == "." || $file == ".."){continue;} //ignore these $filename = explode(".",$file); //seperate filename from extenstion $cnt = count($filename); $cnt--; $ext = $filename[$cnt]; //as above if(strtolower($ext) == strtolower($extension)){ //if the extension of the file matches the extension we are looking for... array_push($list, $file); //...then stick it onto the end of the list array } } if($list[0]){ //...if matches were found... return $list; //...return the array } else {//otherwise... return false; } } //example usage if($win32_exectuables = list_by_ext("exe", "C:\WINDOWS")){ var_dump($win32_exectuables); } else { echo "No windows executables found :(\n"; } ?> ---- Server IP: 87.124.64.96 Probable Submitter: 86.4.53.154 ---- Manual Page -- http://www.php.net/manual/en/function.opendir.php Edit -- https://master.php.net/note/edit/75118 Del: integrated -- https://master.php.net/note/delete/75118/integrated Del: useless -- https://master.php.net/note/delete/75118/useless Del: bad code -- https://master.php.net/note/delete/75118/bad+code Del: spam -- https://master.php.net/note/delete/75118/spam Del: non-english -- https://master.php.net/note/delete/75118/non-english Del: in docs -- https://master.php.net/note/delete/75118/in+docs Del: other reasons-- https://master.php.net/note/delete/75118 Reject -- https://master.php.net/note/reject/75118 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#126477) next »