title: PHP glob() Not recognising files exist
body: <p>I've had a problem with the glob function before and even after many reads through the manual I still seem to stumble at what seem to be basic problems.</p>
  
  <p>In the past I've had problems returning images because the directory I was using was absolute, not relative. I've also had issues with returning images from a foreach because I was looping through but not adding strings to a master string and so only getting the last image etc.</p>
  
  <p>Having experienced these previous issues, I thought I had overcome the problems and knew what would work. The code I'm trying to use will eventually be within a function on a page which is loaded through AJAX. The function is called on the page that is loaded.</p>
  
  <p>However, I can't even get this to work on a static page!</p>
  
  <p>The code I am using is this:</p>
  
  <pre><code>&lt;?php
      $dir = '/portfolio/_images/design/curiousc/gallery/';
      $files = glob(dirname(__FILE__).$dir.'*.{jpg,png,gif,jpeg}', GLOB_BRACE);
      foreach ($files as $file){
          $output = '&lt;img class="project inner-module" src="'.$dir.basename($file).'" alt="'.$file.'" /&gt;';
          $images .= $output;
      };
      var_dump($files); //Returns this: array(0) { }
  
      //I know the $dir is correct as it loads the following code.
      //For sake of reference, I have manually added the filename.
      //$images .= '&lt;img class="project inner-module" src="'.$dir.'1-portrait-900.png" alt="'.$file.'" /&gt;';
  
      //I would eventually have this feature inside a function so would preferably have 'return' rather than 'echo'
      echo $images;
  ?&gt;
  </code></pre>
  
  <p>I've tried the <code>$dir</code> with manual code as a single line and that works, so I know that the directory is listed correctly. I've also done a <code>var_dump($files);</code> which returns "array(0) { }" in the page. Correct me if I'm wrong, but doesn't that mean that it can't find any files?</p>
  
  <p>Any help would be appreciated! The code will eventually list all of the images related to a particular project within my portfolio.</p>
  
  <p>Thanks!</p>
  
  <p>[EDIT]</p>
  
  <p>Adding the file structure and AJAX script for the benefit of @Darhazer's help.</p>
  
  <p><strong>File structure:</strong></p>
  
  <p>/_inc/js/ajaxtrigger.js *This is where the AJAX script is located and working.</p>
  
  <p>/index.php <em>This was the static page that I was testing the glob function.</em></p>
  
  <p>/portfolio/index.php <em>This is loaded into index.php with AJAX</em></p>
  
  <p>/portfolio/project.php <em>This gets loaded into portfolio/index.php which is subsequently within /index.php</em></p>
  
  <p>/portfolio/_images/<em>category</em>/*project*/gallery/ <em>The directory of images for each project</em></p>
  
  <p>I know that project.php is within /portfolio/ but I can't get image references to work whether I set the src to /portfolio/_images/... or just _images/...</p>
  
  <p><strong>The script I am using for AJAX loading is:</strong></p>
  
  <pre><code>$(document).ready(function(){
  
  bindNewClick(); 
  
  function bindNewClick(){
      $('a.ajaxtrigger').on('click', function(){
          // The target for AJAX loading is set with data-target="target-name-here" applied to the &lt;a&gt;
          // The target div needs to have an id that matches #target-target-name-here
          var target = $('div#target-'+$(this).data('target'));
          doAjax($(this).attr('href'), target);
          return false;
      });
  }
  
  function doAjax(url, container){
      if(url.match('^http')){     
          var errormsg = 'Ah! AJAX can\'t load external content';
          container.html(errormsg);
      }
      else {
          $.ajax({
              url: url,
              timeout:5000,
              success: function(data){
                  container.html(data);
                  bindNewClick();
              },
              error: function(req,error){
                  if(error === 'error'){
                      error = req.statusText;
                  };
                  var errormsg = 'Uh oh! There was an error: '+error;
                  container.html(errormsg);
              },
              beforeSend: function(data){
                  container.html('&lt;p&gt;Loading...&lt;/p&gt;');
              }
          });
      };
  };
  </code></pre>
  
  <p>});</p>