【问题标题】:ftping only todays files only仅 ftping 仅今天的文件
【发布时间】:2011-03-24 17:25:01
【问题描述】:

我是 php 新手。有一个任务是从包含文件的服务器上 ftp 一个多星期的今天的文件。如何根据日期和 ftp 选择或过滤文件到我的本地文件夹。 非常感谢您的帮助!

所罗门

【问题讨论】:

    标签: php4


    【解决方案1】:

    链接: http://www.php.net/manual/en/function.ftp-rawlist.php

    你连接到服务器, 获取ftp_rawlist()的文件列表

    并通过ftp_fget() 获取您想要的文件

    例子

    <?php
    
    // set up basic connection
    $conn_id = ftp_connect($ftp_server);
    
    // login with username and password
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    
    // get the file list for /
    $rawfiles = ftp_rawlist($conn_id, '/');
    
    foreach ($rawfiles as $rawfile) {
    # parse the raw data to array
    if(!empty($rawfile)) {
        $info = preg_split("/[\s]+/", $rawfile, 9);       
        $arraypointer[] = array(
            'text'   => $info[8],
            'isDir'  => $info[0]{0} == 'd',
            'size'   => byteconvert($info[4]),
            'chmod'  => chmodnum($info[0]),
            'date'   => strtotime($info[6] . ' ' . $info[5] . ' ' . $info[7]),
            'raw'    => $info
            // the 'children' attribut is automatically added if the folder contains at least one file
        );
    // pseudo code check the date
     if($arraypointer['date'] is today) 
       ftp_fget(file);
    }
    
    // close the connection
    ftp_close($conn_id);
    
    // output the buffer
    var_dump($buff);
    ?>
    

    【讨论】:

      【解决方案2】:

      如果您已经有要检查的文件名,请使用filemtime

      返回文件上次修改的时间,失败时返回 FALSE。时间以 Unix 时间戳的形式返回,适用于 date() 函数。

      要比较今天,您可以使用date('Y-m-d') 来获取今天与date('Y-m-d', filemtime($filename)) 的比较

      要获取文件名,您可以使用readdir 依次读取每个文件名。

      <?php
      if ($handle = opendir('/path/to/files')) {
          echo "Directory handle: $handle\n";
          echo "Files:\n";
      
          while (false !== ($filename = readdir($handle))) {
              echo "$filename\n";
          }
      
          closedir($handle);
      }
      ?>
      

      该手册还有一个FTP example,它应该向您展示如何在找到文件后进行 ftp。

      所以,结合所有这些,你可以得到类似的东西:

      <?php
         // set up basic connection
         $conn_id = ftp_connect($ftp_server); 
      
         // login with username and password
         $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
      
         // check connection
         if ((!$conn_id) || (!$login_result)) { 
            echo "FTP connection has failed!";
            echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
            exit; 
         } else {
            echo "Connected to $ftp_server, for user $ftp_user_name";
         }
      
         if ($handle = opendir('/path/to/files')) {
      
         while (false !== ($filename = readdir($handle))) {
            if (date('Y-m-d') == date('Y-m-d', filemtime($filename))) {
               // upload the file
               $upload = ftp_put($conn_id, $destination_file, $filename, FTP_BINARY); 
      
               // check upload status
               if (!$upload) { 
                  echo "FTP upload has failed!";
               } else {
                  echo "Uploaded $source_file to $ftp_server as $destination_file";
               }
            }
         }
      
         closedir($handle);
      
         // close the FTP stream 
         ftp_close($conn_id); 
      ?>
      

      当然,您需要根据需要填写虚拟值。

      免责声明:我是在notepad++中输入的,没有经过测试!

      【讨论】:

      猜你喜欢
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      相关资源
      最近更新 更多