0

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}\n");

// 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}\n";
}

// Close Connection
ftp_close($conn);
?>
2
  • Please post only relevent code :P BTW what are you sending via $.ajax to checkfolder.php ??? Commented Apr 22, 2015 at 8:31
  • U are not sending any variable or any Form to checkfolder.php Commented Apr 22, 2015 at 8:32

1 Answer 1

2

How to i get the variable so i can just echo it out?

You have to "echo" your html (or json) structure in the php script.

Then with jQuery paste it in the main html:

success: function(result) {
    $('body').html(result);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.