2015-04-22 2 views
0

У меня есть сценарий ajax для папки. Папка будет возвращать $ dir. Страница с сценарием ajax позже получит dir, а затем повторит его позже в php-коде.Получить переменную с ajax

Как получить переменную, чтобы я мог просто повторить ее?

Ajax страница:

<!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> 

Папка наблюдающий, которая возвращает соответствующий каталог:

<?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); 
?> 
+0

Пожалуйста, пост только Релевент код: P Кстати, что вы посылаете через '$ .ajax' в' checkfolder.php' ??? – Umair

+0

U не отправляет какую-либо переменную или какую-либо форму в 'checkfolder.php' – Umair

ответ

2

Как я получить переменную, так что я могу просто повторить его?

Вы должны «эхо» создать структуру html (или json) в скрипте php.

Затем с JQuery вставить его в основной HTML:

success: function(result) { 
    $('body').html(result); 
} 
Смежные вопросы