0

How do I copy files in a directory with filename contains.. see sample below

file_123_XXXXXX.zip where XXXXXX is a random numbers..

I want to copy file_123_XXXXXX.zip from server to same file name to my local folder

the code is working operational if I set the filename exactly the same in the server but what if the file name randomly changes everyday.

thanks in advance..

here is my code:

include("./config.php");

$local_file1 = 'C:\Destination\file_123_XXXXXX.zip'; //how to copy the original filename XXXXX 

if(file_exists($local_file1))
{
    echo "
                $('#getUpdts').attr('disabled','disabled')
                            .addClass('ui-state-disabled');
                $('#proc').removeAttr('disabled')
                        .removeClass('ui-state-disabled');
    ";

    echo "infoMsg('File is already downloaded..')";
}
else
{
    $ftp_user = ftp_user;
    $ftp_pw = ftp_pw;

    $conn_id = ftp_connect('192.xxx.xxx.xxx') or die("Couldn't connect to 192.xxx.xxx.xxx");    

    $server_file1 = "/fromlocation/file_123_XXXXXX.zip"; //the filename with random that i want to get

    $login_result = ftp_login($conn_id, $ftp_user, $ftp_pw);
    if(!file_exists($local_file1))
    {
        $contents = ftp_size($conn_id, $server_file1);
        if ($contents >0) {
            if (ftp_get($conn_id, $local_file1, $server_file1, FTP_BINARY)) {
                echo "infoMsg('Successfully downloaded');";
            } else {
                echo "alertMsg('Unable to download');";
            }
        }else{
            echo "alertMsg('Does not exist.');";
        }
    }
    else
    {
        echo "alertMsg('does not exists');";
    }

    // close the connection
    ftp_close($conn_id);
}
Ryewell
  • 49
  • 1
  • 6
  • Consider getting the last entry file from your server file directory so you don't care anymore about the name. http://stackoverflow.com/questions/1491020/php-get-the-latest-file-addition-in-a-directory – Falt4rm Jun 07 '15 at 14:36
  • good idea.. can you please teach me how can I apply it to may code.. – Ryewell Jun 07 '15 at 15:04

2 Answers2

0

As I proposed in comment you could consider :

  • Connect to the server
  • Find the last modified file in your server file directory
  • Check IF (Local Side) the file already exists

Function's Code :

function get_last_modified_file($dir)
{
   $path = $dir; 

   $latest_ctime = 0;
   $latest_filename = '';    

   $d = dir($path);
   while (false !== ($entry = $d->read())) 
   {
      $filepath = "{$path}/{$entry}";
      // could do also other checks than just checking whether the entry is a file
      if (is_file($filepath) && filectime($filepath) > $latest_ctime) 
      {
         $latest_ctime = filectime($filepath);
         $latest_filename = $entry;
       }
   }
   return $latest_filename;
}

Using your code - It shoud looks like :

include("./config.php");


// Copy - Pasterino the function get_last_modified() here

// Fill variables $server_dir & $local_dir
$ftp_user = ftp_user;
$ftp_pw = ftp_pw;

$server_dir = "the path we will search into the last file/";
$local_dir  = " the destination path/";


$conn_id = ftp_connect('192.xxx.xxx.xxx') or die("Couldn't connect to 192.xxx.xxx.xxx");

$login_result = ftp_login($conn_id, $ftp_user, $ftp_pw);
// Get last modified file
$last_ctime_file = get_last_modified_file($server_dir);

if(file_exists($local_dir . $last_ctime_file"))
{
    // We don't download it / echo  Warning, etc..
    echo "blalbllalba";
}
else
{
    // We download it - Same code you used
    $contents = ftp_size($conn_id, $server_dir . $last_ctime_file);
    if ($contents >0) 
    {
        if (ftp_get($conn_id, $local_dir . $last_ctime_file, 
                              $server_dir . $last_ctime_file, FTP_BINARY)) 
              echo "infoMsg('Successfully downloaded');";
        else
            echo "alertMsg('Unable to download');";
    }
    else
        echo "alertMsg('File is empty.');";
}
ftp_close($conn_id);

Source : Stackoverflow : Get the lastest file in a directory

Community
  • 1
  • 1
Falt4rm
  • 915
  • 6
  • 21
0

List the directory and match the filename using preg_match()

ftp_chdir($conn_id, "/fromlocation/");
foreach (ftp_nlist($conn_id, ".") as $server_file1) {
    if (!preg_match('/^file_123_\d{6}\.zip/i', $server_file1)) continue;
    if (is_file($server_file1)) continue;

    // then the rest of your code...
    $contents = ftp_size($conn_id, $server_file1);
    if ($contents > 0) {
        if (ftp_get($conn_id, $local_file1, $server_file1, FTP_BINARY)) {
            echo "infoMsg('Successfully downloaded');";
        } else {
            echo "alertMsg('Unable to download');";
        }
    } else {
        echo "alertMsg('Does not exist.');";
    }
}
mike.k
  • 3,277
  • 1
  • 12
  • 18