2014-02-06 71 views
2

我正在開發一個應用程序在php中。有很多文件處理。我想在ftp上修改文件夾。目前我正在獲取最後修改的文件。但我想要最後修改文件夾的文件也。如何在ftp上找到最新的最後修改目錄

我的代碼是

$ftp_server='xxxxxxx'; 
$ftp_user_name='xxxxxxxx'; 
$ftp_user_pass='xxxxxxxx';  
$conn_id = ftp_connect($ftp_server); 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

if ((!$conn_id) || (!$login_result)) 
    { 
    echo "FTP connection has failed!"; 
    echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
    exit; 
    } else {} 

    $folder= ftp_pwd ($conn_id ); 

    ftp_pasv($conn_id, true); 

    $get_folder_namesss = ftp_nlist($conn_id, '.'); 
    $folder_name = array() ; 
    $local_file=''; 
    $folder = '/abc/def/ghi/2014/02/'; 
    $handle=''; 
    $time=''; 
    $handle1=''; 
    $files = ftp_nlist($conn_id, $folder); 

    if (!count($files)) { 
    echo "folder is empty"; 
    return false; 
    } 

    $mostRecent = 
    array 
     (
     'time' => 0, 
     'file' => null 
     ); 

    foreach ($files as $file) 
     { 
     if (!preg_match('~\w+.xls$~ism', $file)) continue; 

     $time = ftp_mdtm($conn_id, $file); 

     echo "$file was last modified on : " . date("F d Y H:i:s.", $time); 

     if ($time > $mostRecent['time']) 
      { 
      $mostRecent['time'] = $time; 
      $mostRecent['file'] = $file; 
      } 
     } 

    $local_file = '../ftp/'.basename($mostRecent['file']);       
    if (file_exists($local_file)) 
    {} 
    else 
     { 
     $handle = fopen($local_file, 'w'); 
     if (ftp_fget($conn_id, $handle, $mostRecent['file'], FTP_ASCII, 0)) 
     {} 
     else 
      { 
      return "There was a problem while downloading ".$mostRecent['file']." to $local_file\n"; 
      } 
     fclose($handle); 

     $handle1 = fopen($local_file,"r"); 
     $t=1; 
     $vales = array(); 
     while (($data = fgetcsv($handle1, 1000, ",")) !== FALSE) 
      { 
      $num = count($data); 
      for ($c=0; $c < $num; $c++) 
       { 
       $vales[$t]=$data[$c] ; $t++; 
       } 
      } 
     fclose($handle1); 
     } 

$folder = '/abc/def/ghi/2014/02/';是我的文件夾路徑。最後兩個文件夾是年份和月份。有2011,2012,2013,2014這樣的文件夾,按年份排序。我想找到最新的文件夾,它是2014年。請建議。謝謝。

回答

0

您可以使用stat函數獲取創建文件夾的時間。

$folder = stat('/abc/def/ghi/2014/02/'); 
echo 'Modification time: ' . $folder['mtime']; // will show unix time stamp. 

請嘗試任何事情,你可以做到這個

相關問題