t_miller_3 Posted December 23, 2008 Share Posted December 23, 2008 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 More sharing options...
flyhoney Posted December 23, 2008 Share Posted December 23, 2008 <?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); ?> Link to comment https://forums.phpfreaks.com/topic/138207-solved-need-help-using-ftp-with-php/#findComment-722537 Share on other sites More sharing options...
t_miller_3 Posted December 23, 2008 Author Share Posted December 23, 2008 Okay I've tried that and I'm still getting the same error message. Could it be to do with the web server? Link to comment https://forums.phpfreaks.com/topic/138207-solved-need-help-using-ftp-with-php/#findComment-722544 Share on other sites More sharing options...
flyhoney Posted December 23, 2008 Share Posted December 23, 2008 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) Link to comment https://forums.phpfreaks.com/topic/138207-solved-need-help-using-ftp-with-php/#findComment-722548 Share on other sites More sharing options...
t_miller_3 Posted December 23, 2008 Author Share Posted December 23, 2008 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. Link to comment https://forums.phpfreaks.com/topic/138207-solved-need-help-using-ftp-with-php/#findComment-722560 Share on other sites More sharing options...
flyhoney Posted December 23, 2008 Share Posted December 23, 2008 Looks like a permissions thing to me. Make sure the files you are working with have read/write permissions for your user. Link to comment https://forums.phpfreaks.com/topic/138207-solved-need-help-using-ftp-with-php/#findComment-722562 Share on other sites More sharing options...
t_miller_3 Posted December 23, 2008 Author Share Posted December 23, 2008 Yeah I'm pretty sure they do. If I log into ftp using filezilla I can edit the file with no problems. It's as if it's signing out of ftp straight after it signs in. Link to comment https://forums.phpfreaks.com/topic/138207-solved-need-help-using-ftp-with-php/#findComment-722565 Share on other sites More sharing options...
flyhoney Posted December 23, 2008 Share Posted December 23, 2008 Change the first line to this: <?php $server = '89.XXX.XX.XXX'; $connection = ftp_connect($server) or die("Couldn't connect to $server"); ?> Link to comment https://forums.phpfreaks.com/topic/138207-solved-need-help-using-ftp-with-php/#findComment-722569 Share on other sites More sharing options...
flyhoney Posted December 23, 2008 Share Posted December 23, 2008 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); ?> Link to comment https://forums.phpfreaks.com/topic/138207-solved-need-help-using-ftp-with-php/#findComment-722571 Share on other sites More sharing options...
t_miller_3 Posted December 23, 2008 Author Share Posted December 23, 2008 Ok I tried sending data from my local host to my 1 of my webservers and it worked fine so it must be an issue with the other webserver, thanks for the help even if it wasn't a php issue after all . Link to comment https://forums.phpfreaks.com/topic/138207-solved-need-help-using-ftp-with-php/#findComment-722586 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.