Forum Moderators: coopster
$file = "http://example.com/test.txt";
$handle = fopen($file,"r");
$link = ftp_connect("host");
ftp_login($link,"user","pass");
if (ftp_fput($link, $file, $handle, FTP_ASCII)) {
echo "Successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
ftp_close($conn_id);
fclose($fp)
Hmm...Good luck!
I was thinking opendir could be used to detect the name of the file which changes but always ends with .txt and is the only .txt file.
I could probably do this much myself. What I'm having trouble with is how to connect to the remote server and download the file to my server using PHP and the host address, username and password.
The only other thing is that the text file I'm downloading and the folder it's in might get renamed periodically and I dont want to have to keep manually checking all the time.
Below is the code I have so far but the opendir/readdir only works on the local FTP. How do I get those functions to work on the remote FTP server where the file is that I'm downloading?
$ftp_server="datafeeds.domain.com";
$ftp_user_name="yourusername";
$ftp_user_pass="yourpassword";
$server_file="/folder/filename.txt";
$dir="/";
$local_file="/var/www/html/admin/datafeeds/filename.txt";
$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 as '".$ftp_user_name."'";
}
echo "<BR><BR>";
/* Read the directory to find the text file to download */
if ($handle = opendir('/')) {
echo "Directory handle: $handle<BR>";
echo "Files:<BR><BR>";
while (false!== ($file = readdir($handle))) {
echo "$file<BR>";
}
closedir($handle);
}
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";
}
// close the FTP stream
ftp_close($conn_id);