0

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,

wuno
  • 9,547
  • 19
  • 96
  • 180
  • Can you `var_dump($files);` and post the output? – D4V1D Feb 06 '16 at 06:51
  • Outsite the loop null, inside array(3) { [0]=> string(1) "." [1]=> string(2) ".." [2]=> string(11) "reports.php" } array(3) { [0]=> string(1) "." [1]=> string(2) ".." [2]=> string(11) "reports.php" } – wuno Feb 06 '16 at 06:52
  • Maybe I need to specify only open php files – wuno Feb 06 '16 at 06:52
  • Try switching the `scandir()` function to the [`glob()`](http://php.net/manual/en/function.glob.php) function. `scandir()` only returns *filename*, not the full path. – D4V1D Feb 06 '16 at 06:53
  • May be this will helpful:- http://stackoverflow.com/questions/2922954/getting-the-names-of-all-files-in-a-directory-with-php – Alive to die - Anant Feb 06 '16 at 06:53
  • @D4V1D would you mind answering the question with glob. I cant get a working function and it seems like your comment is the closes to a reasonable answer. – wuno Feb 06 '16 at 07:20

3 Answers3

2

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).
D4V1D
  • 5,805
  • 3
  • 30
  • 65
  • so if I have a variable like this in the file $fileName = "reports"; can I just do echo $fileName; inside the loop to call it and then do what I want with the variable? – wuno Feb 06 '16 at 07:32
  • I don't exactly get what is your prupose with `$fileName`. What does it suppose to contain? And where do you declare this variable? – D4V1D Feb 06 '16 at 07:34
  • in each file in the plugins folder will be a variable called $filename i want to read thane variable from each file and save it to an arrray to make a navigation with that variable being the link name and then the full path of the file being the href. So if you wanted to add a new function or feature to my app you could just write the php file with a few key parameters and then my app will properly display the php file in the content area of the ajax application i am making allowing me to easily add new functions and have them load dynamically. – wuno Feb 06 '16 at 07:37
  • So you want to execute PHP code that will be stored as a string in a variable. [`eval()`](http://php.net/manual/en/function.eval.php) can help you but as stated in the most up-voted comment in this manual section: *"If eval() is the answer, you're almost certainly asking the wrong question." -- Rasmus Lerdorf, BDFL of PHP*. Meanwhile, you should mark your question resolved if my piece of code helped you fetch the files in the `../plugins/` directory as asked. Feel free to ask another question regarding this new aspect of your development :). – D4V1D Feb 06 '16 at 07:40
  • sure thanks bro. Have a good night. now your scaring me that I am doing this a bad way... – wuno Feb 06 '16 at 07:41
  • You most certainly are. You may revisit the conception of your functionality. – D4V1D Feb 06 '16 at 07:50
  • Thank you very much. You made me rethink the whole thing. I appreciate you. – wuno Feb 06 '16 at 08:13
0

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

Armen
  • 4,064
  • 2
  • 23
  • 40
  • Yes but does this read the file names to a directory? I am trying to read a variable which will hold the desired link name for the file in each file which will be names $fileName – wuno Feb 06 '16 at 06:53
  • yes it reaturn full path with file name /var/www/project/file.php, you can take only file name for this via basename("/var/www/project/file.php") it will give you back file.php, for experement you can run var_dump( dirToArray($path) ) and see the result – Armen Feb 06 '16 at 06:55
0

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.

Community
  • 1
  • 1
Sergey Chizhik
  • 617
  • 11
  • 22