Jump to content

using fopen in an FTP connection


rhyspaterson

Recommended Posts

Hey guys,

 

Just a little script i'm writing. I know it's longer than it should be but for the moment i'm just trying to debug what's going on. Here's the code

 

    // set up FTP variables
$ftp_server = "xxx.xxx.xxx.xxx";
$ftp_user_name = "xxxxx";
$ftp_user_pass = "xxxxxxxxx";
$destination = "/xxx/xxx/xxx/xxx/$registrationID/";

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

// check connection
if ((!$conn_id) || (!$login_result)) {
		echo "FTP connection has failed!<br />";
		echo "Attempted to connect to $ftp_server for user $ftp_user_name";
		exit;
	} else {
		echo "Connected to $ftp_server, for user $ftp_user_name<br />";
	}


// enable passive mode
ftp_pasv($conn_id, true);


// change to the users directory
 ftp_chdir($conn_id, $destination);
 echo "we are now in ftp_pwd($conn_id) <br />";

// get the contents of the current directory and place them in an array. If the bbb.txt file exists, the array would output [0]=>  string(10) "aaa.txt" [1]=>  string(12) "bbb.txt". 
$contents = ftp_nlist($conn_id, ".");
 echo "the contents of ftp_pwd($conn_id) are ";
 var_dump($contents);
 echo "<br />";

// we must then search the array for the existence of the key bbb.txt [1].
$key = array_search('bbb.txt', $contents);

// If key == 1, then bbb.txt exists.
if ($key == 1){
echo "The file exists!";

// Open the file
$fh = fopen($destination . 'bbb.txt', 'r') or die("Can't open file " . $destination . "bbb.txt");

// close the file
fclose($fh);

}else{
echo "The file doesn't exist!";
exit();
}

// close the FTP stream
ftp_close($conn_id);

 

The problem is, even though the file exists and i have full RWX permissions, i still cant open it. The script stops at

or die("Can't open file " . $destination . "bbb.txt");

 

Any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/53033-using-fopen-in-an-ftp-connection/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.