Jump to content

Recommended Posts

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

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

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.

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.