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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.