Jump to content

Continous Checking Of Directory


Go to solution Solved by aNubies,

Recommended Posts

Hi again guys :happy-04:, alright I'm close to finish my exercise. This time what I want to know is is it possible to continously check a directory for any changes (changes means if the count of files where decrease or increase)? How to put it into a clock or something using PHP.

 

Or I need to use some sort of javascript?.

 

Please show me how. Thank you very much in advance. ;D

Link to comment
https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/
Share on other sites

Okay, I manage to make a function checkdirectory and execute it using javascript but what i want is execute it base on the time I set. But with my current code it just execute once.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Reduce & Resize Images </title>

<?php
	function CheckDrirectory() {
		$netDir = "Network_Images/*.png";
		$locDir = "Local_Images/";
		
		foreach (glob($netDir) as $net) {
			$files = glob($locDir."/".substr($net, strpos($net, "/") + 1));
			if (empty($files)) {
				$im = imagecreatefrompng($net);
				$new_image = imagecreatetruecolor(50, 50);
				imagecopyresampled($new_image, $im, 0, 0, 0, 0, 50, 50, imagesx($im), imagesy($im));
				$im = $new_image;
				imagepng($im, $locDir.substr($net, strpos($net, "/") + 1), 0);
			}
		}
		exit();
	}
?>

<script type="text/javascript">
	function RepeatingText() {
		document.write("<?php CheckDrirectory(); ?>");
		t = setTimeout(function() {RepeatingText()}, 30000);
	}
</script>

</head>
<body onload="RepeatingText()">

<form action="" method="POST" enctype="multipart/form-data">
	<label name="lblBrowser">Filename :   </label>
    <input type="file" name="fileUpload" id="fileUpload" /><br />

    <input type="submit" value="Upload" id="Upload" />
</form>

</body>
</html>

 

Please point me out on how to execute it continously.

Well CheckDirectory() function does check if the image is exist in the other folder, if its not it will create a copy. So basically i need it to execute continously base on the time i set.

 

The code above just execute at first time around only dunno why.

 

[EDIT]

 

Oh, I see the typo of CheckDirectory spelling, my bad.

Edited by aNubies
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Reduce & Resize Images </title>

<?php
	function CheckDirectory() {
		$netDir = "Network_Images/*.png";
		$locDir = "Local_Images/";
		
		foreach (glob($netDir) as $net) {
			$files = glob($locDir."/".substr($net, strpos($net, "/") + 1));
			if (empty($files)) {
				$im = imagecreatefrompng($net);
				$new_image = imagecreatetruecolor(50, 50);
				imagecopyresampled($new_image, $im, 0, 0, 0, 0, 50, 50, imagesx($im), imagesy($im));
				$im = $new_image;
				imagepng($im, $locDir.substr($net, strpos($net, "/") + 1), 0);
			}
		}
		exit();
	}
?>

<script type="text/javascript">
	function init() {
		document.write("<?php CheckDirectory(); ?>");
		t = setTimeout(function() {init()}, 30000);
	}
</script>

</head>
<body onload="init()">

<form action="" method="POST" enctype="multipart/form-data">
	<label name="lblBrowser">Filename :   </label>
    <input type="file" name="fileUpload" id="fileUpload" /><br />

    <input type="submit" value="Upload" id="Upload" />
</form>

</body>
</html>

 

Here's the corrected CheckDrirectory->CheckDirectory.

How are you determining that it's not working?


first, ensure that your timeout is being called. Do the following:

 

function init() {
        console.log("timeout called");
        CheckDirectory();
        t = setTimeout(init, 30000);
    }

Then open a javascript console in whatever browser you are using, and ensure that "timeout called" is being printed to the browser every 30 seconds. That will tell you if the problem is in your timeouts, or in your other code. Or if it's something different altogether.

Okay after observing something in console and page source, looks like the reason is that after executing it once the source turns into this <document.write("");> at first <document.write("phpcode");> and it turns into nothing. Is it saying that javascript cannot run server side code, but instead use AJAX?

 

Anyone please help me out here.

Here's my current code.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Reduce & Resize Images </title>

<?php
	function CheckDirectory() {
		$netDir = "Network_Images/*.png";
		$locDir = "Local_Images/";
		
		foreach (glob($netDir) as $net) {
			$files = glob($locDir."/".substr($net, strpos($net, "/") + 1));
			if (empty($files)) {
				$im = imagecreatefrompng($net);
				$new_image = imagecreatetruecolor(50, 50);
				imagecopyresampled($new_image, $im, 0, 0, 0, 0, 50, 50, imagesx($im), imagesy($im));
				$im = $new_image;
				imagepng($im, $locDir.substr($net, strpos($net, "/") + 1), 0);
			}
		}
	}
?>

<script type="text/javascript">
	function init() {
		console.log("timeout called");
		document.write("<?php CheckDirectory(); ?>");
		t = setTimeout(function() {init()}, 30000);
	}
</script>

</head>
<body onload="init()">

<form action="" method="POST" enctype="multipart/form-data">
	<label name="lblBrowser">Filename :   </label>
    <input type="file" name="fileUpload" id="fileUpload" /><br />

    <input type="submit" value="Upload" id="Upload" />
</form>

</body>
</html>

I've just realized that your CheckDirectory() function is PHP, while the rest of your code is JavaScript. Sorry, I didn't catch this earlier. You cannot call PHP from JavaScript no matter which way you cut it. You will need to use AJAX for this. This means you will need to write a JavaScript that makes a call to your PHP script on the server, then does something with the output. You can look at jQuery's AJAX functions for an easy-ish way of doing AJAX.

 

In the meantime, that's essentially the end of this thread, as writing AJAX is beyond the scope of what you can expect from a single thread. Good luck!

Edited by haku
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.