
April 4th, 2004, 05:49 AM
|
|
Contributing User
|
|
Join Date: Oct 2003
Posts: 87
Time spent in forums: 21 h 41 m
Reputation Power: 5
|
|
|
ftp_get only txt files in ftp
I need to ftp_connect to a server and dowload all txt files in a directory (which contains jpg and txt files).
The following code will ftp_connect to a server and download (ftp_get) a SPECIFIED file. But I won't know the new filename generated everyday that I need to download. So how do I download ALL txt files in the remote directory and save as same filename in local directory? Thanks.
PHP Code:
// define some variables
$local_file = 'newlocal.txt';
$server_file = 'remotefile.txt';
$ftp_server = "ftp.server.net";
$ftp_user_name = "user";
$ftp_user_pass = "pass";
// set up basic connection
if ($conn_id = ftp_connect($ftp_server)) {
echo "Connected to ".$ftp_server."";
}else{
echo "Can't connect to ".$ftp_server."";
exit();
}
// login with username and password
if ($login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)) {
echo "Logged on to ".$ftp_server."<br>User: ".$ftp_user_name."<br>Pass: ".$ftp_user_pass."";
}else{
echo "Can't logon as User: ".$ftp_user_name."<br>Pass: ".$ftp_user_pass."";
exit();
}
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
exit();
}
// close the connection
if ($conn_id) {
ftp_close($conn_id);
}
|