I want to write a function which reads all the files in a dir and pushes a common variable value in each file to an array.
The idea is kinda to have a wordpress like feature in a way... You add a php file to a plugins folder with certain characteristics. For example every file you add must have a $fileName
variable. My goal here is to grab each of the $fileName
from each file in the dir and push them to an array so I can call on the array to create a navigation. The navigation will then load the php file into a content area when the link is activated with ajax.
My file path is,
/plugins/reports.php
/plugins/RandomPlugin2.php
/plugins/RandomPlugin3.php
I was trying to get this done doing something like this,
in /assets/load-plugins.php
function loadPlugins(){
$files = scandir('../plugins/');
foreach($files as $file) {
if(($file_handle = fopen($file, "r")))
while (!feof($file_handle)) {
$line = fgets($file_handle);
echo $fileName;
}
fclose($file_handle);
}
}
loadPlugins();
But this is the error I get,
Warning: fopen(reports.php) [function.fopen]: failed to open stream: No such file or directory in /Applications/AMPPS/www/wubase/assets/load-plugins.php on line 12
Warning: fclose() expects parameter 1 to be resource, boolean given in /Applications/AMPPS/www/wubase/assets/load-plugins.php on line 17
It tells me there is no such file or directory but it even mentions the file currently in the plugins directory. Could this be a permission problem because I am trying to open a file from a different directory?
Also if someone has a better idea to achieve my goal I am all ears and would appreciate the constructive criticism.
Thanks,