I'm only just starting out on the raod along php, so bear with me.

I've been trying to read the contents of multiple files within collection of sub-directories and testing each to see whether it has a particular value inside it. If it does, I want to create collect of links to those directories and do nothing with the directories that do not have this file in the tested file.

So, I have many directories under /gallery/ and inside each of these I have a file called variable.php, the contents of which is going to vary sometimes (hence the name ). I have some code which will find all the directories and then read the contents into an multi-dimentional array. the code is:

<?PHP 
$gallist=opendir("gallery"); 
$folders=array(); 
while (($folder=readdir($gallist)) !== false) 
{ 
if ($folder != "." and $folder != "..") 
{ 
array_push($folders, $folder); 
} 
} 
closedir($gallist); 
sort($folders); 
foreach ($folders as $folder){ 
$filearray[$folder] = file("gallery/$folder/variables.php"); 
} 
?>

if I use "print_r($filearray);" to see what I have in the array, it prints (test 4 is the name of one of the directories):

Array ( [test4] => Array ( [0] => $title = "Gash $gallery Gallery"; [2] => $exif = 'includes/functions/exif.php'; [3] => $paypal = 'includes/paypal/paypal.php'; [4] => $password = TRUE; [5] => $guest = TRUE; [6] => $uname = "example"; [7] => $pass = "example"; [8] => ?> ) )

The part I am interested in is the "$guest = TRUE;" part. I want to list all the /directories/variables.php having this value.

Any help would be greatly appreciated as I am now stuck. Thank you.

    Assuming the format will stay the same (e.g. it's always "$guest = TRUE;" exactly), this might be what you're after:

    $dirs_matched = array();
    
    foreach($filearray as $dir => $array) {
        if(array_search('$guest = TRUE;', $array) !== FALSE)
            $dirs_matched[] = $dir;
    }
      bradgrafelman wrote:

      Assuming the format will stay the same (e.g. it's always "$guest = TRUE;" exactly), this might be what you're after:

      $dirs_matched = array();
      
      foreach($filearray as $dir => $array) {
          if(array_search('$guest = TRUE;', $array) !== FALSE)
              $dirs_matched[] = $dir;
      }

      Thanks for your help bradgrafelman. Yes, the format will always stay the same.

      I have placed this after my code and print_r($dirs_matched) only outputs Array ( ) which suggests it's empty, though every one should be TRUE and therefore this array populated?

      Any thoughts?

        Not sure what the problem is... did a test case and it worked for me:

        $filearray = array('test4' => array('$title = "Gash $gallery Gallery";', '$exif = \'includes/functions/exif.php\';', '$guest = TRUE;', '$uname = "example";'));
        
        $dirs_matched = array();
        
        foreach($filearray as $dir => $array) {
            if(array_search('$guest = TRUE;', $array) !== FALSE)
                $dirs_matched[] = $dir;
        } 
        
        print_r($dirs_matched); // Array ( [0] => test4 )

        Can you do a var_dump() on $filearray and post the result here (in [code][/code] tags to preserve the formatting)?

          I did var_dump($filearray) and I get this based on there being 2 directories, as you can see:

          array(2) { ["test4"]=> array(15) { [0]=> string(7) " string(115) "//comment out anything not required with a "//" like at the start if this sentance or change falue accordingly \n " [2]=> string(38) "$title = "Gash $gallery Gallery"; \n " [3]=> string(28) "//Require display of exif? " [4]=> string(40) "$exif = 'includes/functions/exif.php'; " [5]=> string(24) "//Require Paypal Cart? " [6]=> string(41) "$paypal = 'includes/paypal/paypal.php'; " [7]=> string(37) "//Password protect - TRUE or FALSE? " [8]=> string(19) "$password = TRUE; " [9]=> string(54) "//Enable / Disable the Guest account - TRUE or FALSE " [10]=> string(16) "$guest = TRUE; " [11]=> string(144) "//Secondary Logon details. If the $guest is commented out, this makes the album only accessable with this username password combo, plus Admin! " [12]=> string(21) "$uname = "example"; " [13]=> string(21) "$pass = "example"; " [14]=> string(2) "?>" } ["test5"]=> array(15) { [0]=> string(7) " string(112) "//comment out anything not required with a "//" like at the start if this sentance or change falue accordingly " [2]=> string(35) "$title = "Gash $gallery Gallery"; " [3]=> string(28) "//Require display of exif? " [4]=> string(40) "$exif = 'includes/functions/exif.php'; " [5]=> string(24) "//Require Paypal Cart? " [6]=> string(41) "$paypal = 'includes/paypal/paypal.php'; " [7]=> string(37) "//Password protect - TRUE or FALSE? " [8]=> string(19) "$password = TRUE; " [9]=> string(54) "//Enable / Disable the Guest account - TRUE or FALSE " [10]=> string(16) "$guest = TRUE; " [11]=> string(144) "//Secondary Logon details. If the $guest is commented out, this makes the album only accessable with this username password combo, plus Admin! " [12]=> string(21) "$uname = "example"; " [13]=> string(21) "$pass = "example"; " [14]=> string(2) "?>" } }
          

          If it helps, this is an example of the file that it is reading:

          <?PHP
          //comment out anything not required with a "//" like at the start if this sentance or change falue accordingly
          $title = "Gash $gallery Gallery";
          //Require display of exif?
          $exif = 'includes/functions/exif.php';
          //Require Paypal Cart?
          $paypal = 'includes/paypal/paypal.php';
          //Password protect - TRUE or FALSE?
          $password = TRUE;
          //Enable / Disable the Guest account - TRUE or FALSE
          $guest = TRUE;
          //Secondary Logon details. If the $guest is commented out, this makes the album only accessable with this username password combo, plus Admin!
          $uname = "example";
          $pass =  "example";
          ?>
          

            The strange thing is, If I do what you have done and directly assign the items in the array, it works fine for me too.

            There is obviously something it doesn't like about the first array, though the output appears to be the same!? Argh!!

              Look at the output very carefully of the var_dump() you posted above (this is exactly why I said var_dump() instead of print_r()): It found a string(16) (a.k.a. a string of 16 characters) of "$guest = TRUE; ".

              Notice that the 16th character is a space, not the semicolon. Thus, you either need to trim spaces off of the end of each element in the array, or you need to include the space in the search parameter of array_search().

                Very well spotted, though I tried this and it makes no difference. And the original file has no space after the ';' so why should there be in the array? If you copy and paste my example file into a text editor you can see there are no spaces.

                I took everything out of the file being read and had just $guest = TRUE; in there, nothing else. It appears that if there is anything on the next line of the file it doesn't work, even if this extra line has been read as a separate value into the array.

                I don't understand why this is. Surely array_search would match "$guest = TRUE;" partially, regardless of space before or after? in_array behaves in exactly the same fashion as array_search.

                  Ah, silly me - not thinking. [man]file/man will return the delimiters on the end of the lines, in otherwords you actually have line breaks at the end of those array elements.

                  You're also right in looking at [man]in_array/man - this would better fit our purposes.

                  What you need to do is add the FILE_IGNORE_NEW_LINES flag to your call to [man]file/man - this will prevent those line breaks from being added. For more information on this flag (and the others), visit the manual page for the [man]file/man function.

                    Yeah, I had a look at FILE_IGNORE_NEW_LINES and it didn't seem to do anything.

                    <?PHP
                    $gallist=opendir("gallery");
                    $folders=array();
                    while (($folder=readdir($gallist)) !== false)
                    {
                    if ($folder != "." and $folder != "..")
                    {
                    array_push($folders, $folder);
                    }
                    }
                    closedir($gallist);
                    sort($folders);
                    echo "<br>";
                    foreach ($folders as $folder){
                    $filearray[$folder] = file("gallery/$folder/variables.php", FILE_IGNORE_NEW_LINES);
                    }
                    $dirs_matched = array();
                    foreach($filearray as $dir => $array) {
                        if(in_array('$guest = TRUE;', $array) !== FALSE)
                            $dirs_matched[] = $dir;
                    }
                    ?>
                    

                    It would seem that this flag doesn't necessarily work very well and that it is better to use rtrim(). I have tried using this in my foreach just after I have used file()

                    	foreach ($filearray as $folder => $value){
                    		$value = rtrim($value);
                    

                    But still nothing.

                      Is there any chance you can attach one of these PHP files your script is reading (that has the $guest = TRUE; setting) to your next post? You'll have to rename it to a .txt file.

                        I downloaded that file, and ran this code:

                        $filearray['test'] = file('variables.txt', FILE_IGNORE_NEW_LINES);
                        
                        $dirs_matched = array();
                        foreach($filearray as $dir => $array) {
                            if(in_array('$guest = TRUE;', $array) !== FALSE)
                                $dirs_matched[] = $dir;
                        }
                        
                        print_r($dirs_matched);

                        The result was as expected:

                        Array
                        (
                            [0] => test
                        )

                        I'm not sure why it's not working for you - is that the exact file you're using? There may be some encoding issue or something... other than that, I'm not sure.

                          Yep, exact same file. How odd. I have tried this on both my local testing server (Windows - WAMP) and remote host (UNIX based - here) - same on both.

                          I even copied your code snippet (so I can isolate it from everything else), downloaded the file I gave you the link to (just to make sure) and put them into a new directory all on their own. I'm at a complete loss...

                          Many thanks for your help anyway. I shall post back here if I ever find what the problem is.

                            Write a Reply...