1

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>');
            }
        });
    };
};

});

joeallam
  • 57
  • 2
  • 10
  • And does a directory `/portfolio/` really exist at the servers root directory? Because that's what your absolute path specifies. (They're not relative to your websites document_root.) – mario Jul 06 '13 at 16:00
  • possible duplicate of [Server Document Root Path in PHP](http://stackoverflow.com/questions/15211231/server-document-root-path-in-php) – mario Jul 06 '13 at 16:01
  • Yes, the directory does exist as tested with the manually written img src. I've since amended the glob to include dirname(__FILE__) as well. – joeallam Jul 06 '13 at 16:41
  • The HTML `` uses the path web-relative. PHP file function use server-absolute paths. It has been explained to you two times now. Where's the difficulty? Did you really not look at the given help link? – mario Jul 06 '13 at 16:47
  • Ok, forgive me for not understanding, I was following the direction of @Darhazer who mentioned `dirname(__FILE__)`. When I followed the link through to someone else's question I didn't know which parts were relevant because trying to match an answer to my question with an answer from someone else's question isn't always easy. Is it really that hard to drop your sarcasm and simple explain, "you need to use `$_SERVER["DOCUMENT_ROOT"]` and oh by the way have a look at this link." Thanks in any case, I got there. – joeallam Jul 06 '13 at 17:03
  • In my eyes, I'm not sure on the difference between server-absolute and web-relative. Wouldn't both of my instances load from the root /portfolio/_images/ ? – joeallam Jul 06 '13 at 17:09

1 Answers1

1

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)

Dylan Valade
  • 5,565
  • 6
  • 42
  • 56
Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
  • Thanks, this worked on the static page setup! Can I ask how my path is not relative? I thought absolute paths were http://... It's solved my issue on the static page, but when implemented on the AJAX called page within the function it does not work. I know the function is working correctly because other elements are within it. Are there any things to watch out for when using it on an AJAX loaded page? – joeallam Jul 06 '13 at 16:15
  • absolute URL is with http://; absolute path (on the filesystem) is with /. If you call exactly the same script in the browser (not via ajax, just copy and paste url), does it work? – Maxim Krizhanovsky Jul 06 '13 at 16:22
  • Opening the file directly within the browser still says array(0) { }. I've tried adjusting the path to match the folder structure but it still won't read the files. – joeallam Jul 06 '13 at 16:28
  • Please post the file structure and the script you are calling – Maxim Krizhanovsky Jul 06 '13 at 16:36
  • Got it working now, using `$_SERVER["DOCUMENT_ROOT"]` solved the issue on the AJAX called page. Thanks for your help in the right direction, though! – joeallam Jul 06 '13 at 17:07