4

I have written Following code to connect to FTP, which gives me an error "Warning: preg_match() [function.preg-match]: Unknown modifier 'p'"

    <?php

// define some variables
$ftp_server="www.abc.com";
$ftp_user_name="username";
$ftp_user_pass="password";
$local_file = 'L021-D8127-BLUEWASH-2T.jpg';
$server_file = '/abc/photos/L021-D8127-BLUEWASH-2T.jpg';

$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

//
//Enable PASV ( Note: must be done after ftp_login() )
//
$mode = ftp_pasv($conn_id, TRUE);

// get contents of the current directory
$contents = ftp_nlist($conn_id, "abc/photos");

// output $contents
//var_dump($contents);

foreach($contents as $file){
if(!preg_match("/L021-D8127-BLUEWASH-([1-9]|10)(T|S)\.jpg/i", $file)){
    // continute if its not the file I want to download

    continute;
}
// try to download file and save to $local_file
if (ftp_get($conn_id, $local_file, file, FTP_BINARY)) {
    echo "Successfully written to $local_file\n";
}
else {
    echo "There was a problem\n";
}
}

// close the connection
ftp_close($conn_id);

?>

On the server in "photos" folder i have multiple instances of same image but with different name sequence like

L021-D8127-BLUEWASH-2T.jpg
L021-D8127-BLUEWASH-3T.jpg
L021-D8127-BLUEWASH-4T.jpg
and so on till 10T.jpg

And similarly ...

L021-D8127-BLUEWASH-2S.jpg
L021-D8127-BLUEWASH-3S.jpg
L021-D8127-BLUEWASH-4S.jpg
and so on till 10S.jpg

My Question is with one single FTP connection open how do i do to ..
1) Check if all occurenes of L021-D8127-BLUEWASH-( 1 t0 10 )T.jpg & L021-D8127-BLUEWASH-( 1 t0 10 )S.jpg Exists.
2) If the Image(s) Exists Download all files matching
3) I do not want to use 20 FTP Connections simultaneously ?

user580950
  • 3,558
  • 12
  • 49
  • 94

1 Answers1

5

So what you want to do in your scrip there is call ftp_nlist

$contents = ftp_nlist($conn_id, ".");

And loop through the result set of that.

foreach ($contents as $file) {
   $local_file = '';
   $server_file = '';
   ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)
}

etc...

tlenss
  • 2,609
  • 2
  • 22
  • 26