motorcity Posted February 3, 2013 Share Posted February 3, 2013 I guess I need some help here. This is going to be cron job once I get it going. The source server is Microsoft-IIS/7.5 (we're getting pretty large text files that are in a directory. I.E. in manual ftp you need to cd /folder/webfiles/) The destination server is Apache/2.2.17 (Unix) PHP/5.2.9 File & folder permissions set to 777 So this is what I've been getting; Warning: ftp_get(file.txt) [function.ftp-get]: failed to open stream: Permission denied in /home/name/public_html/cron_transfer_texts.php on line 36 Warning: ftp_get() [function.ftp-get]: Error opening file.txt in /home/name/public_html/cron_transfer_texts.php on line 36 (Line 36 is the ftp_get) I wasn't sure if I had a connection or not so I tried; // get contents of the current directory $contents = ftp_nlist($conn_id, "/folder/webfiles/"); // output $contents var_dump($contents); It sat there and chewed on it for several minutes, but just finished without putting anything on the screen. Then I tried this; if (!($type = ftp_systype($conn_id))) { echo "FTP connection has failed! Trying again.\n"; $conn_id = ftp_connect($ftp_server); $login_result = ftp_login($conn_id, $ftp_user, $ftp_pass); }else{ echo ("Nothing to report");} and all I get is; Nothing to report <?php ini_set('display_errors',1); error_reporting(E_ALL); // define some variables $folder_path = "/home/name/public_html/test"; //test is a directory 777 $local_file = "file.txt"; $server_file = "file.txt"; //-- Connection Settings $ftp_server = "ftp.msserver.com"; // Address of FTP server. 00.000.000.000 //tried name & ip same results $ftp_user_name = "username"; // Username $ftp_user_pass = "password"; // Password #$destination_file = "FILEPATH"; // set up basic connection $conn_id = ftp_connect($ftp_server) or die("did not connect"); // login with username and password $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); // try to download $server_file and save to $local_file // must be either FTP_ASCII or FTP_BINARY if (ftp_get($conn_id, $local_file, $server_file, FTP_ASCII)) { if ((!$conn_id) || (!$login_result)) { die("FTP connection has failed !"); } echo "Current directory: " . ftp_pwd($conn_id) . "\n"; // try to change the directory to somedir if (ftp_chdir($conn_id, "folder/webfiles")) { echo "Current directory is now: " . ftp_pwd($conn_id) . "\n"; } else { echo "Couldn't change directory\n"; } echo "Successfully written to $local_file\n"; } else { echo "There was a problem\n"; } // close the connection ftp_close($conn_id); ?> Quote Link to comment https://forums.phpfreaks.com/topic/273989-ftp_get-server-to-server-microsoft-to-unix/ Share on other sites More sharing options...
kicken Posted February 3, 2013 Share Posted February 3, 2013 Try creating the local file first so you can tell if that is failing or something with the ftp. You can use ftp_fget to download to a file stream, eg: $lf = fopen($local_file, 'w'); if (!$lf){ die('Failed to create local file.'); } if (!ftp_fget($conn_id, $lf, $remote_file, FTP_ASCII)){ die('Failed to download file from FTP server.'); } fclose($lf); Quote Link to comment https://forums.phpfreaks.com/topic/273989-ftp_get-server-to-server-microsoft-to-unix/#findComment-1409890 Share on other sites More sharing options...
motorcity Posted February 3, 2013 Author Share Posted February 3, 2013 Thanks, that's moving closer. Now I know it's about changing directories on the remote server. Quote Link to comment https://forums.phpfreaks.com/topic/273989-ftp_get-server-to-server-microsoft-to-unix/#findComment-1409917 Share on other sites More sharing options...
kicken Posted February 3, 2013 Share Posted February 3, 2013 Why are you trying to change directories after you download the file? Perhaps you should be doing that before the ftp_get? Quote Link to comment https://forums.phpfreaks.com/topic/273989-ftp_get-server-to-server-microsoft-to-unix/#findComment-1409921 Share on other sites More sharing options...
motorcity Posted February 3, 2013 Author Share Posted February 3, 2013 Makes perfect sense, only my method doesn't seem to work. Pretty much anywhere I've tried it. <?php ini_set('display_errors',1); error_reporting(E_ALL); // define some variables $folder_path = "/home/name/public_html/test/"; $local_file = "file.txt"; $server_file = "file.txt"; //-- Connection Settings $ftp_server = "ftp.msserver.com"; // Address of FTP server. $ftp_user_name = "u"; // Username $ftp_user_pass = "p"; // Password #$destination_file = "FILEPATH"; // set up basic connection $conn_id = ftp_connect($ftp_server); // login with username and password $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); if(ftp_chdir($conn_id, "/folder/webfiles/")) { echo "Current directory is now: " . ftp_pwd($conn_id) . "\n"; }else{ echo "Couldn't change directory\n"; } $lf = fopen($local_file, 'w'); if (!$lf){ die('Failed to create local file.'); } if (!ftp_fget($conn_id, $lf, $server_file, FTP_ASCII)) { die('Failed to download file from FTP server.'); } fclose($lf); // close the connection ftp_close($conn_id); ?> Here's what I'm getting; Current directory is now: /folder/webfiles Warning: ftp_fget() [function.ftp-fget]: Opening ASCII mode data connection for skudescadd.txt(216014 bytes). in /home/name/public_html/test/cron_transfer_texts.php on line 26 Failed to download file from FTP server. Quote Link to comment https://forums.phpfreaks.com/topic/273989-ftp_get-server-to-server-microsoft-to-unix/#findComment-1409929 Share on other sites More sharing options...
kicken Posted February 3, 2013 Share Posted February 3, 2013 Some google searches on that warning message suggest you may need to enable passive transfer mode using ftp_pasv. Do so just after you call ftp_login. Quote Link to comment https://forums.phpfreaks.com/topic/273989-ftp_get-server-to-server-microsoft-to-unix/#findComment-1409931 Share on other sites More sharing options...
motorcity Posted February 3, 2013 Author Share Posted February 3, 2013 Thanks again! But I'm still stuck though; Warning: ftp_fget() [function.ftp-fget]: Can't open data connection. Most of what I'm finding on this is the ftp_pasv that you suggested and I'm already using. It's not a time out issue even though this script runs for about 2-3 minutes, this is the smallest file of the group of 9 text files I need to deal with. Same results whether I use the ftp.name or ip Same also whether its ascii or binary. Quote Link to comment https://forums.phpfreaks.com/topic/273989-ftp_get-server-to-server-microsoft-to-unix/#findComment-1409945 Share on other sites More sharing options...
motorcity Posted February 4, 2013 Author Share Posted February 4, 2013 I'm still plugin away at this, still getting this error; Warning: ftp_fget() [function.ftp-fget]: Can't open data connection The script actually runs about 75 seconds just about every time. I tested manually and this file takes 4 seconds to download. I've tried ftp_get same result, ftp_nlist and ftp_rawlist and it still takes the same amount of time... Something isn't right. <?php ini_set('display_errors',1); error_reporting(E_ALL); // define some variables $folder_path = "/home/name/public_html/test/"; $local_file = "file.txt"; // $server_file = "file.txt"; //-- Connection Settings $ftp_server = "msserver"; // Address of FTP server. $ftp_user_name = "u"; // Username $ftp_user_pass = "p"; // Password // set up basic connection $conn_id = ftp_connect($ftp_server); // I checked, port is 21 // login with username and password $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); ftp_pasv($conn_id, true); if(ftp_chdir($conn_id, "folder/webfiles/")) { echo "Current directory is now: " . ftp_pwd($conn_id) . "\n"; } // ftp_pasv($conn_id, true); $lf = fopen($local_file, 'w'); if (!$lf){ die('Failed to create local file.'); } if (!ftp_fget($conn_id, $lf, $server_file, FTP_ASCII)) { die('Failed to download file from FTP server.'); } // ftp_get($conn_id, $local_file, $server_file, FTP_ASCII); // close the connection ftp_close($conn_id); fclose($lf); ?> Quote Link to comment https://forums.phpfreaks.com/topic/273989-ftp_get-server-to-server-microsoft-to-unix/#findComment-1410090 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.