tags:

views:

20

answers:

2

Hi, I am new to php. have a task to ftp only todays files from the server containing files for over a week. how do i select or filter the files based on date and ftp to my local folder. Help is much appreciated !!

Soloman

A: 

If you already have the filenames to check, use filemtime

Returns the time the file was last modified, or FALSE on failure. The time is returned as a Unix timestamp, which is suitable for the date() function.

To compare that today, you can use date('Y-m-d') to get today compare to date('Y-m-d', filemtime($filename))

To get the filenames, you could use readdir to read each one in turn.

<?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);
}
?>

The manual also has an FTP example which should show you how to ftp the file once found.

So, combining all this, you could get something like:

<?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); 
?>

Of course, you will need to fill in the dummy values as appropriate.

Disclaimer: I typed this in notepad++ and it is not tested for any mistakes!

Dan McGrath
I think I've misinterpreted your question. If so, see haim's answer. http://stackoverflow.com/questions/3396685/ftping-only-todays-files-only/3396742#3396742
Dan McGrath
A: 

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

you connect to the server , get the list of the files with ftp_rawlist()

and get only file you want via ftp_fget()

example

<?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);
?>
Haim Evgi