简体   繁体   中英

Read PHP Files From A Directory And Push A Common Variable In Each File To An Array

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,

Try this:

function loadPlugins() {
    $files = glob('../plugins/');

    foreach($files as $file) {
        if(($file_handle = fopen($file, "r"))) {
            while (!feof($file_handle)) {
                $line = fgets($file_handle);
                echo $line;
            }
        }
        fclose($file_handle);
    }
}
  • Switch the function to glob() instead of scandir() (the former returns the full Unix path when the latter returns only the filename).
  • Take the habit of always use curly brackets for your if() statements, even when they are optional.
  • $fileName was not set, I assumed you meant $line (which is set right above).

Seems you have problem with file path, you can define and use this function to read your directory all files with ab

function dirToArray($dir) {

  $result = array();      
  $cdir = scandir($dir);

  foreach ($cdir as $key => $value){
    if (!in_array($value,array(".",".."))){
        if (is_dir($dir . DIRECTORY_SEPARATOR . $value)){
            $result = array_merge(
                   $result, 
                   dirToArray($dir . DIRECTORY_SEPARATOR . $value) 
            );
        }else{
            $result[] = $dir . DIRECTORY_SEPARATOR . $value;
        }
    }
  }

   return $result;
}

it will give you all files with absolute path located in that mentioned directory or subsirectories

scandir returns just name of file or directory(not full path). So, for example

.
├── 1.txt
├── 2.txt
├── 3.txt
└── public
    └── script.php

Your script in public folder, required files in ../ folder. scandir('../') will return

array (
  0 => '.',
  1 => '..',
  2 => '1.txt',
  3 => '2.txt',
  4 => '3.txt',
  5 => 'public',
);

So you need function, that returns full path to files, or make this path by yourself.


UPD: as D4V1D mentioned, glob() will solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM