Forum Moderators: coopster

Message Too Old, No Replies

Downloading/Uploading Files To/From Remote FTP Servers

         

dougmcc1

1:41 am on Jun 23, 2006 (gmt 0)

10+ Year Member



How do I opendir on a remote server? I'm able to gain access with ftp_login. I want to copy a file from that server to another server, without having to save the file to my local hard drive first.

Any ideas? Is this even possible?

eelixduppy

4:11 am on Jun 23, 2006 (gmt 0)



I believe if you can use fopen [us3.php.net] on a remote file, then you can use ftp_fput [us3.php.net] to accomplish what you want. Maybe this will work?:


$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!

dougmcc1

7:41 pm on Jun 25, 2006 (gmt 0)

10+ Year Member



Well maybe I should provide more information. What I want to do is take a file on a remote server and download it to my local server via FTP.

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.

eeek

11:34 pm on Jun 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



All you have to do is use ftp_login to login to the ftp server. Then ftp_get to grab the file. When you're done use ftp_close to shut down the connection.

eeek

11:38 pm on Jun 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I should add that you need to do an ftp_connect before the login. Read the documentation at [us3.php.net...] for more info.

dougmcc1

4:24 am on Jun 26, 2006 (gmt 0)

10+ Year Member



Yeah it was easy enough to download the file. I guess no upload is required since the script is running on the FTP server that I'm saving the file to.

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