aNubies Posted March 20, 2013 Share Posted March 20, 2013 Hi again guys , 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. Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/ Share on other sites More sharing options...
monkeypaw201 Posted March 20, 2013 Share Posted March 20, 2013 You can't run client-side code with PHP. Your best bet is to use jquery's ajax() function to query a php page and then compare it to the stored/cached value of when the page was loaded. Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1419804 Share on other sites More sharing options...
aNubies Posted March 25, 2013 Author Share Posted March 25, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420841 Share on other sites More sharing options...
haku Posted March 25, 2013 Share Posted March 25, 2013 CheckDrirectory() ?? Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420842 Share on other sites More sharing options...
aNubies Posted March 25, 2013 Author Share Posted March 25, 2013 (edited) 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 March 25, 2013 by aNubies Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420843 Share on other sites More sharing options...
haku Posted March 25, 2013 Share Posted March 25, 2013 Re-read my last post. Letter-by-letter. Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420844 Share on other sites More sharing options...
aNubies Posted March 25, 2013 Author Share Posted March 25, 2013 <!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. Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420845 Share on other sites More sharing options...
haku Posted March 25, 2013 Share Posted March 25, 2013 Your problem is solved? Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420846 Share on other sites More sharing options...
aNubies Posted March 25, 2013 Author Share Posted March 25, 2013 Nope not yet, the code above just executed once, thinking of my code it should be execute every base time set in setTimeout(), but it didn't. Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420847 Share on other sites More sharing options...
aNubies Posted March 25, 2013 Author Share Posted March 25, 2013 removing the exit(); doesn't make any changes, still it execute once. Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420850 Share on other sites More sharing options...
haku Posted March 25, 2013 Share Posted March 25, 2013 Try the following. There is no need for a call to document.write(). function init() { CheckDirectory(); t = setTimeout(init, 30000); } Side note, exit() is not a JavaScript function. Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420854 Share on other sites More sharing options...
aNubies Posted March 25, 2013 Author Share Posted March 25, 2013 Thank you for the response, but it doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420858 Share on other sites More sharing options...
haku Posted March 25, 2013 Share Posted March 25, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420859 Share on other sites More sharing options...
aNubies Posted March 25, 2013 Author Share Posted March 25, 2013 Sorry, never knew about console.log, but anyway, i just tested it and it says CheckDirectory not defined. So i place it back in php tag and re-run it, and it works fine however only once not with the desire every 30secs count. Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420860 Share on other sites More sharing options...
aNubies Posted March 25, 2013 Author Share Posted March 25, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420863 Share on other sites More sharing options...
haku Posted March 25, 2013 Share Posted March 25, 2013 Please look at my previous code that does not use document.write(). Document.write() is outdated javascript and there is no reason to be using it. You should be using the format I showed. Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420867 Share on other sites More sharing options...
aNubies Posted March 25, 2013 Author Share Posted March 25, 2013 Yeah I recently use the format that you've given to me Sir Haku, however; the console says CheckDirectory is not defined and it doesn't execute at all. Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420868 Share on other sites More sharing options...
haku Posted March 25, 2013 Share Posted March 25, 2013 Show us your current code. Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420869 Share on other sites More sharing options...
haku Posted March 25, 2013 Share Posted March 25, 2013 Also you can just call me haku - I have not been knighted (yet). Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420870 Share on other sites More sharing options...
aNubies Posted March 25, 2013 Author Share Posted March 25, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420871 Share on other sites More sharing options...
haku Posted March 25, 2013 Share Posted March 25, 2013 (edited) 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 March 25, 2013 by haku Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420872 Share on other sites More sharing options...
Solution aNubies Posted March 25, 2013 Author Solution Share Posted March 25, 2013 well, thank you haku for the info, you've anwer my question since i have a feeling that seeing my source code, javascript seems unable to call the php second time around. Thank you again haku. Quote Link to comment https://forums.phpfreaks.com/topic/275899-continous-checking-of-directory/#findComment-1420873 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.