Jump to content

How to run php files continiously(listfiles in folder without having to refres)


vij

Recommended Posts

I am new to php. I have a small php script that checks for the number of files in a folder and lists them. I am using XAMPP.

 

 

     
   <?php
    //full path to your folder from root
    set_time_limit(0);
    $path = "c:/vij";
    
    // Open the folder
    $dir_handle = @opendir($path) or die("Unable to open $path");
    
    // Loop through the files
    while ($file = readdir($dir_handle)) {
    
    if($file == "." || $file == ".." || $file == "index.php" )
    
    continue;
    echo "<a href=\"$file\">$file</a><br>";
    
    }
    
    // Close
    closedir($dir_handle);
    
    ?>

 

Now my question is how do I get the script to run continuosly and list files in the directory whenever some updates(adding or removing files) are done to the directory. Right now I need to keep refreshing the page. Please help!

This can't be done with PHP alone. You could make it refresh periodically to update, or you could use AJAX and run something every X seconds/minutes to check if anything has changed, if so update the content.

AJAX!

 

function Post(){
var contentType = "application/x-www-form-urlencoded; charset=UTF-8";
var ajaxRequest;
try{
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			alert("Your Browser Doesn't support AJAX.");
			return false;
		}
	}
}
return Array(ajaxRequest,contentType);
}

setInterval("loadFiles()", 5000);  // Run function every 5 seconds...

function loadFiles(){
var connect = Post();
connect[0].onreadystatechange = function(){
	if(connect[0].readyState == 4){
		document.getElementById('files').innerHTML = connect[0].responseText;
	}
}
var aboutMe = document.getElementById('aboutMe').value;
var va = '';
connect[0].open("POST", '/process/getImages.php', true);  // Place your file here....
connect[0].setRequestHeader("Content-Type", connect[1]);
connect[0].send(va);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.