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

标签 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 年的。请提出建议。谢谢。

最佳答案

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

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

请尝试任何你能用它做的事情

关于php - 如何在ftp上找到最新的最后修改目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21601805/

相关文章:

php - ("SQLSTATE[HY000] [1045] Access denied for user ' root' @'localhost' (使用密码 : NO)")

php - 更新没有HTML form标签的数据库表并从MySQL表中获取特定值并显示

php - 用 preg_match() 匹配括号

drupal - 如何允许用户将大图像(> 5 GB)上传到 Drupal 站点?

asp-classic - FileSystemObject 是否知道文件不完整?

c# - Gmt php 或 UTC C# 通过 SOAP 等效

php - 使用 UNION ALL 时出现问题

ftp - 421 抱歉,此服务器不接受明文 session 。

mysqldump使用ftp命令备份到远程服务器

PHP ftp_chdir - 无法屏蔽错误