simple ftp fopen() not working??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cloudbase
Forum Newbie
Posts: 15
Joined: Tue Dec 13, 2011 12:11 pm

simple ftp fopen() not working??

Post by cloudbase »

Let me apologize in advance for asking this simple of a question, but I am pulling my hair out and it's not working.
Trying to copy 14 gif files located on #1 server and write to #2 server for to use later.... this is as far as I've made it.
Local File--> C:\Documents and Settings\Ron.PC192371242527\Desktop\jsnh-000.gif
Server File--> jsnh-000.gif
connected--> Resource id #2
logged on result 1

( ! ) Warning: fopen(jsnh-000.gif) [function.fopen]: failed to open stream: No such file or directory in C:\wamp\www\GetFtpFileWriteToFtp.php on line 18

Code: Select all

// define some variables
$ftp_server = "ftp.server.com";
$server_file = "jsnh-000.gif";
$local_file = "C:\Documents and Settings\Ron.PC192371242527\Desktop\jsnh-000.gif";
echo "Local File--> $local_file --Server File--> $server_file<br>\n";

// set up basic connection
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect");
echo "connected--> $conn_id<br>\n";

// login with username and password
$login_result = ftp_login($conn_id, "xxxx","xxxx") or die("Couldn't login");
echo "logged on result $login_result<br>\n";

// try to download $server_file and save to $local_file
if (fopen($server_file, 'r')) {
    echo "Successfully opened $server_file<br>\n";
	fclose($server_file);
	echo "sucessfully closed $server_file";
} else {
    echo "There was a problem<br>\n";
}

// close the connection
ftp_close($conn_id);
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: simple ftp fopen() not working??

Post by Christopher »

You need to use ftp_get() to download the file, not fopen().
(#10850)
cloudbase
Forum Newbie
Posts: 15
Joined: Tue Dec 13, 2011 12:11 pm

Re: simple ftp fopen() not working??

Post by cloudbase »

Thanks Chris,
Bear with me please I'm very new at this....
I was under the impression that fopen() works within a ftp wrapper.
I replaced the previous code section as you suggest,

Code: Select all

// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
    echo "Successfully opened $server_file<br>\n";
} else {
    echo "There was a problem<br>\n";
}
with the following result

( ! ) Warning: ftp_get() [function.ftp-get]: Type set to I. in C:\wamp\www\GetFtpFileWriteToFtp.php on line 18

p.s. note, there is no problem accessing the file using browser.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: simple ftp fopen() not working??

Post by Christopher »

You might want to look at ftp_pasv().
cloudbase wrote:p.s. note, there is no problem accessing the file using browser.
FTP and accessing something with a browser are very different. I assume that you are able to get the file with a FTP client.
(#10850)
cloudbase
Forum Newbie
Posts: 15
Joined: Tue Dec 13, 2011 12:11 pm

Re: simple ftp fopen() not working??

Post by cloudbase »

Hi All - problem solved for now.
There's this thing called ftp_pasv.
So here's the complete working code. (note ftp_nlist is just me getting some confirmation)

Code: Select all

// define some variables
$ftp_server = "ftp.server.com";
$server_file = "jsnh-000.gif";
$local_file = "C:\Documents and Settings\Ron.PC192371242527\Desktop\jsnh-000.gif";
echo "Local File--> $local_file<br>\n 
	Server File--> $server_file<br>\n
	Server --> $ftp_server<br>\n";

// set up basic connection
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect");
echo "connected--> $conn_id<br>\n";

// login with username and password
$login_result = ftp_login($conn_id, "xxxx","xxxx") or die("Couldn't login");
echo "logged on result $login_result<br>\n";

// turn passive mode on
ftp_pasv($conn_id, true);

// get contents of the current directory
$contents = ftp_nlist($conn_id, ".");

// output $contents
var_dump($contents);


// close the connection
ftp_close($conn_id);

Thanks to all the guys (and gals) on his forum....... YOUR A GREAT RESOURCE
cloudbase
Forum Newbie
Posts: 15
Joined: Tue Dec 13, 2011 12:11 pm

Re: simple ftp fopen() not working??

Post by cloudbase »

Thanks Chris !!!!!
Post Reply