Skip to content

Instantly share code, notes, and snippets.

@loopmode
Last active August 29, 2015 14:04
Show Gist options
  • Save loopmode/b3c13cf7ba3e1ec7ccd0 to your computer and use it in GitHub Desktop.
Save loopmode/b3c13cf7ba3e1ec7ccd0 to your computer and use it in GitHub Desktop.
PHP cached json from multiple files
<?php
/**
* Serves a JSON array with the contents of multiple JSON files.
*
* Scans a $path for $target JSON files in nested folders,
* collects the contents of each of those files and outputs
* them as a JSON array.
* ($path/{*dir}/$target)
*
* The result is stored in a cache file along with the md5
* hash of the directory.
*
* If the contents of $path have not changed (based on the md5),
* the cached results are returned instead of scanning
* the filesystem again.
*
* @author Jovica Aleksic
*/
// User-defined variables
//=====================================================
// The path to the videos
$path = "files/uploads/videos/";
// The name of the target files that should be collected ($path/*/$target)
$target = "description.json";
// caching setup
//=====================================================
$cachedir = $_SERVER["DOCUMENT_ROOT"] . "/cache/videolist/";
is_dir($cachedir) || mkdir($cachedir, 0700, true);
$cachefile = $cachedir . "cache.json";
$md5_cachefile = $cachedir . "cache.md5";
$md5_now = MD5_DIR($path);
$md5_old = file_exists($md5_cachefile) ? file_get_contents($md5_cachefile) : "";
// serve the results
//=====================================================
header('Content-Type: application/json');
if ($md5_now === $md5_old) {
echo file_get_contents($cachefile);
}
else {
$result = fetch_json($path, $target);
file_put_contents($cachefile, $result);
file_put_contents($md5_cachefile, $md5_now);
echo $result;
}
die;
// HELPER FUNCTIONS
//=====================================================
/**
* Collects multiple JSON files from a path and combines them to a JSON-encoded array.
* @param String $path The path to the directory that contains sub-directories with JSON files.
* @param String $file The name of the JSON file that should be loaded from each sub-directory
* @return String An JSON-encoded array containing the contents of all found JSON files
*/
function fetch_json($path, $file) {
$list = array();
foreach (scandir($path) as $dir)
{
if (!is_dir("$path/$dir") || $dir == "." || $dir == "..") continue;
$list[] = json_decode( file_get_contents("$path/$dir/$file") );
}
return json_encode($list);
}
/**
* Creates the md5 hash of an entire directory.
* http://php.net/manual/en/function.md5-file.php#75393
* @param String $dir The path to the directory to be indexed
* @return String The md5 hash of the directory
*/
function MD5_DIR($dir)
{
if (!is_dir($dir))
{
return false;
}
$filemd5s = array();
$d = dir($dir);
while (false !== ($entry = $d->read()))
{
if ($entry != '.' && $entry != '..')
{
if (is_dir($dir.'/'.$entry))
{
$filemd5s[] = MD5_DIR($dir.'/'.$entry);
}
else
{
$filemd5s[] = md5_file($dir.'/'.$entry);
}
}
}
$d->close();
return md5(implode('', $filemd5s));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment