Jump to content

[SOLVED] Need help using ftp with php


t_miller_3

Recommended Posts

I've just started to learn some php and seem to have hit a brick wall already.

 

I'm trying to write the contents of a file on one server to a file on another server using ftp.

 

Here's the code.

 

ftp_login(ftp_connect("89.XXX.XX.XXX"), "myusername", "mypassword");
ftp_fput(ftp_connect("89.XXX.XX.XXX"), "folder/file.ini", fopen("file.ini", "r"), FTP_ASCII) or die("Couldn't write to file");
ftp_close(ftp_connect("89.XXX.XX.XXX"));

 

The error message I get is :

 

Warning: ftp_fput() [function.ftp-fput]: Please login with USER and PASS. in /home/timm/public_html/formtest/ftptest.php on line 20

 

Can anyone tell me where I'm going wrong?

Link to comment
https://forums.phpfreaks.com/topic/138207-solved-need-help-using-ftp-with-php/
Share on other sites

<?php
$connection = ftp_connect("89.XXX.XX.XXX");
ftp_login($connection, "myusername", "mypassword");
ftp_fput($connection, "folder/file.ini", fopen("file.ini", "r"), FTP_ASCII) or die("Couldn't write to file");
ftp_close($connection);
?>

Yeah, I would use an FTP client to test that an FTP connection to that server is even possible.  If you are running linux you can use the ftp command or if using windows download something like Filezilla. (although windows has a built in client too i think)

I'm sure a connection is possible because I connect to it regularly with filezilla. I've updated the code so it now looks like this :

 

<?php
$connection = ftp_connect("89.XXX.XX.XXX");
ftp_login($connection, "myusername", "mypassword") or die("Couldn't connect to server");
ftp_fput($connection, "folder/file.ini", fopen("file.ini", "r"), FTP_ASCII) or die("Couldn't write to file");
ftp_close($connection);
?>

 

If it couldn't connect I think it would display the 'or die' statement but it doesn't, I'm still getting :

 

Warning: ftp_fput() [function.ftp-fput]: Access denied. in /home/timm/public_html/formtest/ftptest.php on line 4

 

It's like it's requesting me to login again to do the ftp_fput but in all the tutorials I've looked at my code is correct.

And let's add some more error checking:

 

<?php
$server = '89.XXX.XX.XXX';
$user = 'myusername';
$pass = 'mypassword';

$connection = ftp_connect($server) or die("Couldn't connect to $server");

ftp_login($connection, $user, $pass) or die("Couldn't login with username: $user");

$fp = fopen("file.ini", "r");

ftp_fput($connection, "folder/file.ini", $fp, FTP_ASCII) or die("Couldn't write to file");

ftp_close($connection);

fclose($fp);
?>

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.