如何在ftp上找到最新的最后修改目录

时间:2014-02-06 11:35:00

标签: php ftp

我正在用php开发一个应用程序。有很多文件处理。我想在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年。请建议。感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用stat功能获取文件夹创建时间。

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

请尝试使用此功能