Skip to content

Instantly share code, notes, and snippets.

@yitsushi
Created September 8, 2014 10:06
Show Gist options
  • Save yitsushi/7b1fd8148cad5638246a to your computer and use it in GitHub Desktop.
Save yitsushi/7b1fd8148cad5638246a to your computer and use it in GitHub Desktop.
xhr file process sample
<?php
if (isset($_GET['generate'])) {
file_put_contents("generatedReports/" . $_GET['generate'] . ".csv.pending", (string)date(time()));
echo json_encode(array('path' => "generatedReports/" . $_GET['generate'] . ".csv", 'filename' => $_GET['generate'] . ".csv"));
die();
}
if (isset($_GET['check'])) {
$files = array();
$dir = opendir('generatedReports');
while (false !== ($file = readdir($dir))) {
if (strpos($file, ".csv") === false) { continue; }
if (strpos($file, '.pending') !== false) {
if (rand() % 5 === 0) {
rename("generatedReports/" . $file, "generatedReports/" . str_replace('.pending', "", $file));
$files[] = array('path' => "generatedReports/" . str_replace('.pending', "", $file), "filename" => str_replace('.pending', "", $file));
} else {
$files[] = array("path" => "", "filename" => str_replace('.pending', "", $file));
}
} else {
$files[] = array('path' => "generatedReports/" . $file, "filename" => $file);
}
}
echo json_encode($files);
die();
}
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
</head>
<body>
<div class="jumbotron">
<div class="container">
<h1>Reports</h1>
<h2>File List:</h2><?php $num = 0; ?>
<p id="reportList">
<?php $dir = opendir('generatedReports'); ?>
<?php while (false !== ($file = readdir($dir))): ?>
<?php if (strpos($file, ".csv") === false) { continue; } else { $num++; } ?>
<?php if (strpos($file, ".pending") === false): ?>
<a class="label label-success glyphicon glyphicon-link" href="<?php echo $file ?>"><?php echo $file; ?></a>
<?php else: ?>
<span class="label label-default glyphicon glyphicon-question-sign"><?php echo str_replace(".pending", "", $file); ?></span>
<?php endif; ?>
<?php endwhile; ?>
<?php closedir($dir); ?>
<?php if ($num < 1) { echo "<h3><small>Empty</small></h3>"; } ?>
</p>
<div class="row">
<div class="col-md-4">
<input type="text" id="filename" placeholder="Filename" class="form-control">
</div>
<div class="col-md-3">
<a href="#" class="btn btn-primary" onclick="return generate(this);">Generate</a>
</div>
</div>
</div>
</div>
<script>
var generate = function(link) {
var fileName = jQuery('#filename').val();
if (!fileName) { return false; }
jQuery.get("xhr.php?generate=" + fileName, function(resp) {
jQuery('#reportList').append(jQuery("<span class='label label-default glyphicon glyphicon-question-sign'>" + resp.filename + "</span><span> </span>"));
jQuery('#filename').val("");
}, "json");
return false;
};
var check = function() {
jQuery.get('xhr.php?check', function(resp) {
jQuery('#reportList').html('');
for(var i = 0, _l = resp.length; i < _l; i++) {
if (resp[i].path !== "") {
jQuery('#reportList').append(jQuery("<a class='label label-success glyphicon glyphicon-link' href='" + resp[i].path + "'>" + resp[i].filename + "</a><span> </span>"));
} else {
jQuery('#reportList').append(jQuery("<span class='label label-default glyphicon glyphicon-question-sign'>" + resp[i].filename + "</span><span> </span>"));
}
}
}, "json");
};
setInterval(check, 2000);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment