fopen - Read PHP Files From A Directory And Push A Common Variable In Each File To An Array -
i want write function reads files in dir , pushes common variable value in each file array.
the idea kinda have wordpress feature in way... add php file plugins folder characteristics. example every file add must have $filename
variable. goal here grab each of $filename
each file in dir , push them array can call on array create navigation. navigation load php file content area when link activated ajax.
my file path is,
/plugins/reports.php /plugins/randomplugin2.php /plugins/randomplugin3.php
i trying done doing this,
in /assets/load-plugins.php
function loadplugins(){ $files = scandir('../plugins/'); foreach($files $file) { if(($file_handle = fopen($file, "r"))) while (!feof($file_handle)) { $line = fgets($file_handle); echo $filename; } fclose($file_handle); } } loadplugins();
but error get,
warning: fopen(reports.php) [function.fopen]: failed open stream: no such file or directory in /applications/ampps/www/wubase/assets/load-plugins.php on line 12 warning: fclose() expects parameter 1 resource, boolean given in /applications/ampps/www/wubase/assets/load-plugins.php on line 17
it tells me there no such file or directory mentions file in plugins directory. permission problem because trying open file different directory?
also if has better idea achieve goal ears , appreciate constructive criticism.
thanks,
try this:
function loadplugins() { $files = glob('../plugins/'); foreach($files $file) { if(($file_handle = fopen($file, "r"))) { while (!feof($file_handle)) { $line = fgets($file_handle); echo $line; } } fclose($file_handle); } }
- switch function
glob()
instead ofscandir()
(the former returns full unix path when latter returns filename). - take habit of always use curly brackets
if()
statements, when optional. $filename
not set, assumed meant$line
(which set right above).
Comments
Post a Comment