1

with $result ok and $inclusion being an actual file name

I would like to do this :

while ($row = mysqli_fetch_array($result)){

 $inclusion = $row['filename'] ;

 // check if file exists here

 include $inclusion ;

}

But it does not loop ... and instead stops after the first inclusion ... why ? Thank you !

Sagar Naliyapara
  • 3,971
  • 5
  • 41
  • 61
  • because the file does not exists and it prints an error? you should check with if (file_exists($inclusion)) include $inclusion ; – Dennis Stücken Apr 03 '15 at 16:28
  • What do you have in your files? Also do you get any errors? – Rizier123 Apr 03 '15 at 16:28
  • From the code you show I would say it does not start *after* the first inclusion but inside the included file. It's also not file existance because that would only give a warning but no fatal. Alernatively it would be that there is only one row. You should enable error reporting to the highest level and the display of error as well as the logging to catch errors earlier: [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) – hakre Apr 03 '15 at 16:32
  • @Dennis S : Yes, I do the file_exists check before including – patHgaronne Apr 03 '15 at 16:40
  • @Dennis S and Rizier123 : I have checked with very basic file content like echo("something"). Only the first include will output ... – patHgaronne Apr 03 '15 at 16:40
  • @patHgaronne Well I can't see your monitor, so maybe this works for you: `while ($row = mysqli_fetch_array($result)){ $files[] = $row['filename']; } $files = array_unique(array_filter($files, function($v){ return file_exists($v); })); array_map(function($v){ require_once($v); }, $files);` – Rizier123 Apr 03 '15 at 16:42
  • Do we know that our results contain more than one file? – BigScar Apr 03 '15 at 16:44
  • @hakre : "..there is only one row" No, unfortunately ! I have checked that too, there are 2 rows in the particular test I'm running ... error reporting is on too ... So frustrating ! – patHgaronne Apr 03 '15 at 16:51
  • @all ! Thank you for beeing so fast !! – patHgaronne Apr 03 '15 at 16:52
  • Can you echo $inclusion and show us the fie lname and path? – BigScar Apr 03 '15 at 16:55
  • @BigScar : row1 > C:/xampp/htdocs/promenade3/php/traitements/entrees/classification.php row2 > C:/xampp/htdocs/promenade3/php/traitements/entrees/proprietes.php – patHgaronne Apr 03 '15 at 17:01
  • @patHgaronne Try again with my posted code above^ But change this line: `require_once($v);` to `require_once(__DIR__ . "/" . basenmae($v));` – Rizier123 Apr 03 '15 at 17:07
  • @Rizier123 : I had to make the variables 'global' in the included files but your solution works fine !! Thank you so much !! I'll have to understand why now ... – patHgaronne Apr 03 '15 at 17:11
  • @patHgaronne Should I convert it to an answer? – Rizier123 Apr 03 '15 at 17:11
  • 1
    @Rizier123 Yes please ! I was trying to find out how to accept your answer ... – patHgaronne Apr 03 '15 at 17:18

1 Answers1

0

This should work for you:

Here I first add all files to the array $files. After this I array_filter() all files out which doesn't exists and take only unique files with array_unique().

At the end I just loop through all files with array_map() then since you don't have the path form the server root, but from the server disk, I just take the basename() (means the file name) and concatenate it with __DIR__ to include it.

<?php

    while ($row = mysqli_fetch_array($result)){
        $files[] = $row['filename'];
    }

    $files = array_unique(array_filter($files, function($v){
        return file_exists($v);
    }));

    array_map(function($v){
        require_once(__DIR__ . "/" . basename($v));
    }, $files);

?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156