Call image from ANY subfolder of ‘Uploads’

Solution:1

Doing this would be very cumbersome and terribly slow. You would have to serve all the possible paths from server side, and then walk through them using Ajax requests or something. It’s not a good solution.

The problem should be solvable more easily by changing the way WordPress organizes your uploads. The setting is in Settings -> Media:

I don’t know, however, what happens if you do this on an existing install that already has uploads. You may have to reorganize/re-upload all your posts’ content to be able to use this (I’m pretty sure WP won’t automatically re-organize.) Use at your own risk and make backups.

Solution:2

I think this would do the trick. Post a request to the server (through AJAX). The server returns the path to all files with commas to separate them, once back on the client jQuery splits this string of data at the commas and puts the full paths in an array. When this is done you can simply loop through the array and create an image element for every element of the array, or do whatever you want.

The PHP file

<?php
function ListFiles($dir) {

    if($dh = opendir($dir)) {

        $files = Array();
        $inner_files = Array();

        while($file = readdir($dh)) {
            if($file != "." && $file != ".." && $file[0] != '.') {
                if(is_dir($dir . "/" . $file)) {
                    $inner_files = ListFiles($dir . "/" . $file);
                    if(is_array($inner_files)) $files = array_merge($files, $inner_files); 
                } else {
                    array_push($files, $dir . "/" . $file);
                }
            }
        }

        closedir($dh);
        return $files;
    }
}


foreach (ListFiles('wp-content/uploads') as $key=>$file){
    echo $file .",";
}
?>

SOURCE: http://tycoontalk.freelancer.com/php-forum/41811-list-files-in-directory-sub-directories.html#post202723

jQuery:

  $(function(){
        $.post("getallfiles.php",{/*post data to server if needed*/}, function(data){
          //Splitting with comma expecting it not to be in filenames and directory names.
          var uploads = data.split(','); 
          //uploads[0] would return the first image's path if I'm correct.
        })
  });

I think this works, I didn’t try it out though, all you need to do is loop through the uploads array and do whatever you want with the imagepath.