I have a ajax script to a folderwatcher. The folderwatcher will return $dir. The page with the ajax script will later on get the dir and then echo it out later in the php code.
How to i get the variable so i can just echo it out?
Ajax page:
<!DOCTYPE html>
<head>
<script type="text/javascript">
$(document).ready(function(){
setInterval(function(){
//code goes here that will be run every 5 seconds.
$.ajax({
type: "POST",
url: "checkfolder.php",
success: function(result) {
//something that will get the varaible?
}
});
}, 5000);
});
</script>
</head>
<body>
<?php
echo $dir;
?>
</body>
</html>
The folder watcher, that returns the right dir:
<?php
// Configuration ///////////////////////////////////////////////////////////////
$host ='ftp.dodododo.com';
$port = 21;
$user = 'xoxo';
$pass = 'xoxo';
$cache_file = 'ftp_cache';
$dir = "../img/uploads/viktigt/";
$filesfound = false;
//kollar upp mappen viktigt
if (!is_dir_empty($dir)) {
$filesfound = true;
$filetype = "img";
}
//kollar igenom om det finns filer i standrard
if ($filesfound == false){
$dir = "../img/uploads/standard/";
if (!is_dir_empty($dir)) {
$filesfound = true;
$filetype = "img";
}
}
//Funktion som kollar mapparna
function is_dir_empty($dir) {
if (!is_readable($dir)) return NULL;
$handle = opendir($dir);
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
return FALSE;
}
}
return TRUE;
}
// Main Run Program ////////////////////////////////////////////////////////////
// Connect to FTP Host
$conn = ftp_connect($host, $port) or die("Could not connect to {$host}
");
// Login
if(ftp_login($conn, $user, $pass)) {
ftp_pasv($conn,true); // Ny rad lägg in den
// Retrieve File List
$files = ftp_nlist($conn, $dir);
// Filter out . and .. listings
$ftpFiles = array();
foreach($files as $file)
{
$thisFile = basename($file);
if($thisFile != '.' && $thisFile != '..') {
$ftpFiles[] = $thisFile;
}
}
// Retrieve the current listing from the cache file
$currentFiles = array();
if(file_exists($cache_file))
{
// Read contents of file
$handle = fopen($cache_file, "r");
if($handle)
{
$contents = fread($handle, filesize($cache_file));
fclose($handle);
// Unserialize the contents
$currentFiles = unserialize($contents);
}
}
// Sort arrays before comparison
sort($currentFiles, SORT_STRING);
sort($ftpFiles, SORT_STRING);
// Perform an array diff to see if there are changes
$diff = array_diff($ftpFiles, $currentFiles);
if(count($diff) > 0)
{
echo "1";//New file/deleted file
}
else{
$diff = array_diff($currentFiles,$ftpFiles);
if(count($diff) > 0)
{
echo $dir;
}
}
// Write new file list out to cache
$handle = fopen($cache_file, "w");
fwrite($handle, serialize($ftpFiles));
fflush($handle);
fclose($handle);
}
else {
echo "Could not login to {$host}
";
}
// Close Connection
ftp_close($conn);
?>