Read PHP Files From A Directory And Push A Common Variable In Each File To An Array
<p>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. </p>

<p>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 <code>$fileName</code> variable. My goal here is to grab each of the <code>$fileName</code> 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. </p>

<p>My file path is, </p>

<pre><code>/plugins/reports.php
/plugins/RandomPlugin2.php
/plugins/RandomPlugin3.php
</code></pre>

<p>I was trying to get this done doing something like this,</p>

<p>in <code>/assets/load-plugins.php</code></p>

<pre><code>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();
</code></pre>

<p>But this is the error I get,</p>

<pre><code>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
</code></pre>

<p>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? </p>

<p>Also if someone has a better idea to achieve my goal I am all ears and would appreciate the constructive criticism.</p>

<p>Thanks,</p>