Refresh Page When Directory Content Changes
Solution 1:
With the PHP function stat, you can get the information of the directory.
The document is here : http://php.net/manual/en/function.stat.php
For example you can get the modification time and directory size as below:
dir_stat.php
<?php$stat = stat('\path\images');
echo'time: ' . $stat['mtime']; /* time of last modification (Unix timestamp) */echo'size: ' . $stat['size']; /* size in bytes */?>
And on the client side, you can retrieve the directory information and compare them at fixed intervals. The example code might goes like below with jQuery.
<scriptlanguage="javascript">var myVar=setInterval(function(){chekUpdate()},5*60*1000); // at 5 minutes intervalsvar stat_old = "";
functionchekUpdate()
{
$("#stat").load("/path/to/dir_stat.php",function(){
var stat_new = $("#stat").html();
if((stat_old != "") && (stat_old != stat_new)){
refreshSlideShow();
}
stat_old = stat_new;
});
}
functionrefreshSlideShow()
{
// you can refresh your slideshow here.
}
</script><body><divid="stat"></div></body>
Solution 2:
The idea is that you make an asynchronous request to the server every N minutes, and the server returns the directory contents (for example filenames in a JSON array): if they changed since the last update, you modify your DOM accordingly. So you need only to implement a PHP service to list the contents of the target directory and return the serialized filenames list. No hashes and no inotify needed.
Note that this design is unnecessary heavy on resources usage (mainly bandwidth, but also CPU because of the high number of incoming requests), because if the directory is not changed for 365 days, still a client issues lots of useless requests. The resource usage can obviously be optimized by introducing so-called server push: your client may maintain a long connection to the server, and, when something change, the server itself delivers the fresh data to all connected clients.
Unfortunately, server-sent events are not usually developed in PHP because of poor environment support, so you'll have to switch to some other technology (google for comet servers, or websocket capable servers), and that's why inotify will never come to play with PHP (even if it has a PHP wrapper).
Solution 3:
no ajax calls needed just update src and pass a random query string to force browser to re-look for images on server and update them on browser window
<scripttype='text/javascript'>functionupdateImages()
{
document.getElementById('img_slide1_element_id').src = 'Slide1.JPG?' + Math.random();
document.getElementById('img_slide2_element_id').src = 'Slide2.JPG?' + Math.random();
window.setTimeout(function(){
updateImages();
}, 60000 * 10);
}
window.setTimeout(function(){
updateImages();
}, 60000 * 10);
</script>
Solution 4:
Here is my attempt:
<?php$filename = "plaatjes/Dia1.JPG";
$myFile = "plaatjes/timestamp";
if (file_exists($myFile)) {
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
}
else {
$theData = 'x'; }
if (file_exists($filename)) {
$timestamp = date ("F d Y H:i:s.", filemtime($filename));
if ($theData === $timestamp) {
echo"True";
}
else {
echo"False";
$fh = fopen($myFile, 'w') ordie("can't open file");
fwrite($fh, $timestamp);
fclose($fh);
}
}
?>
Post a Comment for "Refresh Page When Directory Content Changes"