Search code examples
phpajaximageglob

PHP glob() Not recognising files exist


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.

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.

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.

However, I can't even get this to work on a static page!

The code I am using is this:

<?php
    $dir = '/portfolio/_images/design/curiousc/gallery/';
    $files = glob(dirname(__FILE__).$dir.'*.{jpg,png,gif,jpeg}', GLOB_BRACE);
    foreach ($files as $file){
        $output = '<img class="project inner-module" src="'.$dir.basename($file).'" alt="'.$file.'" />';
        $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 .= '<img class="project inner-module" src="'.$dir.'1-portrait-900.png" alt="'.$file.'" />';

    //I would eventually have this feature inside a function so would preferably have 'return' rather than 'echo'
    echo $images;
?>

I've tried the $dir with manual code as a single line and that works, so I know that the directory is listed correctly. I've also done a var_dump($files); 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?

Any help would be appreciated! The code will eventually list all of the images related to a particular project within my portfolio.

Thanks!

[EDIT]

Adding the file structure and AJAX script for the benefit of @Darhazer's help.

File structure:

/_inc/js/ajaxtrigger.js *This is where the AJAX script is located and working.

/index.php This was the static page that I was testing the glob function.

/portfolio/index.php This is loaded into index.php with AJAX

/portfolio/project.php This gets loaded into portfolio/index.php which is subsequently within /index.php

/portfolio/_images/category/*project*/gallery/ The directory of images for each project

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/...

The script I am using for AJAX loading is:

$(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 <a>
        // 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('<p>Loading...</p>');
            }
        });
    };
};

});


Solution

  • You are providing absolute path, starting from the /root of the system What you want to do is probably a scan of a subfolder, e.g. relative path Change $dir to dirname(__FILE__) . '/portfolio/_images/design/curiousc/gallery/' (assuming that the portfolio is a subfolder of the one in which your script is located)