Re: open a file in a folder without knowing the filename
| From: | Daniel Brown | Date: | Thu, 21 Jun 2007 14:34:52 +0000 |
| Subject: | Re: open a file in a folder without knowing the filename | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-257550@lists.php.net to get a copy of this message | ||
On 6/21/07, Edward Kay <edward@labhut.com> wrote:
-----Original Message----- From: Graham Shaw [mailto:zaknafien@blueyonder.co.uk] I'm probably missing something really obvious here but how would I go about opening a file or multiple files in a folder without knowing the filename ahead of time? Read the files in the directory and place the names of the ones you want into an array using the directory functions: http://uk.php.net/manual/en/ref.dir.php Edward -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.phpDISCLAIMER: Once again, I'm programming in the email window without testing the code at all. In theory, it should work, but there's probably a bug somewhere. <? function ls($d='',$f='') { // Lists files in a directory. /*
Default directory is the PWD.
Default files are all (non-hidden) files.
Usage examples:
ls(); // List all non-hidden files in the current directory.
ls('/share/files'); // All non-hidden files in /share/files.
ls('/home/me/www/','*.php'); // All .php files in /home/me/www/.
*/
$d && (substr($d,-1,1) != "/") ? $d .= "/" : ''; // Addstrailing slash if needed.
$d && !is_dir($d) ? $err = "Not a directory!" : ''; // Checksfor existence.
!$err ? exec('ls '.$d.$f.' 2>&1',$ret) : $ret = $err; //
Redirects error output.
// If you want to list files AND directories, comment this section out
// and change return $files; below to return $ret;
for($i=0;$i<count($ret);$i++) {
is_file($ret[$i]) ? $files[] = $ret[$i] : '';
}
return $files; // Returns an array of files.} print_r(ls()); ?> -- Daniel P. Brown [office] (570-) 587-7080 Ext. 272 [mobile] (570-) 766-8107