2010-08-03 84 views
1

我是新來的php。有一項任務只能從包含文件的服務器獲取超過一週的ftp文件。我如何根據日期和ftp選擇或過濾文件到我的本地文件夾。 非常感謝幫助!僅ftp僅今天文件

所羅門

回答

0

如果你已經有文件名來檢查,用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); 
?> 

當然,您需要填寫虛值適當。

免責聲明:我在記事本++中鍵入了它,並沒有測試任何錯誤!

+0

我想我誤解了你的問題。如果是這樣,請看haim的答案。 http://stackoverflow.com/questions/3396685/ftping-only-todays-files-only/3396742#3396742 – 2010-08-03 13:22:20

0

鏈接: 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); 
?>