Skip to content

Instantly share code, notes, and snippets.

@sleepdeprecation
Created December 23, 2011 22:27
Show Gist options
  • Save sleepdeprecation/1515547 to your computer and use it in GitHub Desktop.
Save sleepdeprecation/1515547 to your computer and use it in GitHub Desktop.
jQuery, PHP, and a directory of images

This is a relatively simple group of scripts I setup for a future jekyll based portfolio. I'm using it to easily display every image inside of a directory on a page.

This assumes that your images are in [root]/img/[directory]

<?php
// used to display a json file with all of the images in a directory listed as img[x].url
$idir = $_GET['dir'];
$idir = "img/$idir/*";
echo "{\n";
echo "\"imgs\": [\n";
$ar = glob($idir);
for ($i = 0; $i < count($ar); $i++) {
echo "\t\t{\n\t\t\t\"url\": \"" . $ar[$i] . "\"\n\t\t}" . ($i < count($ar) - 1 ? "," : "") . "\n";
}
echo "\t]\n}";
?>
<!-- working under the assumption that you've already got jQuery on the page. Also, this is only the relevant part of the html page -->
<div id="img"></div>
<script>
$.getJSON("../php/images.json.php?dir=[name of directory inside 'img/']",
function(data) {
$.each(data.imgs, function(i, val) {
$("#img").html($("#img").html() + "<img src=\"" + val.url + "\" />");
})
}
);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment