View Full Version : taking all the files in one directory and assigning them their own variable ?
GregT
November 9th, 2002, 20:19
how would i go about taking all the files in one directory and assigning them their own variable ? i need to have every filename.tmpl assigned to their own $filename_tmpl variable ? how (if possible) would i do this ? it needs to be in php.
Salam
November 10th, 2002, 13:23
$h=opendir('.');
while (($f=readdir($h))!==false) if(is_file($f)) ${str_replace('.', '_', $f)}=$f ;
GregT
November 10th, 2002, 15:25
a little more explanation plz ?
Salam
November 10th, 2002, 21:57
$h=opendir('.') : Opens current directory and returns a handle ($h) for later uses .
while (($f=readdir($h))!==false) : Reads contents of that directory ( readdir($h) ) until all files & subdirs were be read (!==false) and assignes the file name to $f .
if(is_file($f)) : Checkes if $f is a file (it may be a sub directory of the main dir ('.') ) .
${str_replace('.', '_', $f)}=$f : Makes a variable ($VARNAME) with the name of $f which the '.'s are replaced by '_' (str_replace('.', '_', $f)) .
Powered by vBulletin® Version 4.1.7 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.