Page 1 of 3
Uploading file into ftp server
Posted: Thu Jul 21, 2011 1:59 pm
by YoussefSiblini
Hi,
I am trying to upload an excel file into an ftp server, I connected to the ftp but am getting an error saying that the file couldn't upload.
I am new to php and I need to sort this ASAP please help, here is the code:
//Send the FTP Connection and send the Excel file to the ftp server
$ftp_server = "xxxxxxxxx";
$ftp_user = "xxxx";
$ftp_pass = "xxxxxxxxxx";
$file = 'xls/AB-MXRPayment.xls';
$fp = '/AB-MXRPayment.xls';
// set up a connection or die
$conn_id = ftp_connect($ftp_server , 20 ) or die("Couldn't connect to $ftp_server");
// try to login
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
echo "Connected as $ftp_user@$ftp_server\n";
} else {
echo "Couldn't connect as $ftp_user\n";
}
if (@ftp_put($conn_id, $fp, $file, FTP_ASCII)) {
echo "Successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
and here is the answer I am getting:
Connected as user1@80.45.12.74 There was a problem while uploading xls/AB-MXRPayment.xls
Youssef
Re: Uploading file into ftp server
Posted: Thu Jul 21, 2011 2:11 pm
by YoussefSiblini
This code is uploading the file into a port 21 ftp server but not in port 20 ftp server, I thought this information will help you more?
Re: Uploading file into ftp server
Posted: Thu Jul 21, 2011 7:20 pm
by McInfo
XLS files should probably be transferred in FTP_BINARY mode.
Do you know what the @ operator does?
Re: Uploading file into ftp server
Posted: Fri Jul 22, 2011 4:13 am
by YoussefSiblini
No, no idea what @ operator does, what confusing me is that this code is working and uploading the xls file in port 21 but not port 20.
Youssef
Re: Uploading file into ftp server
Posted: Fri Jul 22, 2011 12:02 pm
by McInfo
If you don't know what something does, research it: search the Web; read the manual. Curiosity is an essential trait for a successful programmer.
The @ operator suppresses error messages. Without error messages, it is difficult to diagnose a problem. So, the first thing to do is remove both of the at-signs and see what errors appear.
Based on the output you say the script delivers, it looks like the ftp_put() function is where the problem occurs.
Re: Uploading file into ftp server
Posted: Fri Jul 22, 2011 12:18 pm
by YoussefSiblini
Hi, you are right, I took them before off and I got an error and I researched that error and I never sorted it out, So at the end I put them back, OK now I am in same problem again I took them off and I am getting this error:
Warning: ftp_put() [function.ftp-put]: Opening data channel for file transfer. in /home/abmxrcom/public_html/ipn.php on line 189
and this is the line 189: if (ftp_put($conn_id, $fp, $file, FTP_ASCII))
Youssef
Re: Uploading file into ftp server
Posted: Fri Jul 22, 2011 2:19 pm
by McInfo
Try enabling passive mode with
ftp_pasv().
Re: Uploading file into ftp server
Posted: Fri Jul 22, 2011 5:16 pm
by YoussefSiblini
Hi Thank you for all your help, I enabled passive mode and I am getting this error:
Connected as user1@80.45.12.74
Warning: ftp_put() [function.ftp-put]: php_connect_nonb() failed: Operation now in progress (115) in /home/abmxrcom/public_html/ipn.php on line 189
Warning: ftp_put() [function.ftp-put]: Type set to I in /home/abmxrcom/public_html/ipn.php on line 189
There was a problem while uploading xls/AB-MXRPayment.xls
If you want, I can give you the ftp server details, I will really appreciate if you can help me in this.
Youssef
Re: Uploading file into ftp server
Posted: Fri Jul 22, 2011 6:59 pm
by McInfo
Why not use port 21?
Re: Uploading file into ftp server
Posted: Sat Jul 23, 2011 12:44 am
by YoussefSiblini
It will not connect to the server if I used port 21, I have tried !!!
Re: Uploading file into ftp server
Posted: Sat Jul 23, 2011 10:01 am
by McInfo
YoussefSiblini wrote:This code is uploading the file into a port 21 ftp server but not in port 20 ftp server,
YoussefSiblini wrote:this code is working and uploading the xls file in port 21 but not port 20.
YoussefSiblini wrote:It will not connect to the server if I used port 21,
I'm confused. Twice you said it works on port 21; now you say it doesn't.
Re: Uploading file into ftp server
Posted: Sat Jul 23, 2011 11:37 am
by YoussefSiblini
Yes I can see that I really confused you sorry for that I will clear things for you.
Lets say there are 2 servers I want to connect and upload the file into:
1- Server A (I use port 21 to connect and the file upload normally and every thing work good)
2- Server B (I use port 20 because if used port 21 to connect it doesn't connect, in this server where I am talking about now, I am able to connect to the server using port 20 but getting message saying: Connected as user1@80.45.12.74
Warning: ftp_put() [function.ftp-put]: php_connect_nonb() failed: Operation now in progress (115) in /home/abmxrcom/public_html/ipn.php on line 189
Warning: ftp_put() [function.ftp-put]: Type set to I in /home/abmxrcom/public_html/ipn.php on line 189
There was a problem while uploading xls/AB-MXRPayment.xls
Youssef
Re: Uploading file into ftp server
Posted: Sat Jul 23, 2011 11:43 am
by McInfo
Is it necessary to be connected to both servers at the same time?
Re: Uploading file into ftp server
Posted: Sat Jul 23, 2011 12:08 pm
by YoussefSiblini
No Server A is a test server, but I want to use server B.
Re: Uploading file into ftp server
Posted: Sat Jul 23, 2011 1:32 pm
by McInfo
So, connect to Server A. Do stuff. Close the connection. Connect to Server B. Do stuff. Close the connection. For both connections, connect on port 21. Will that work?
FTP uses port 21 to communicate control information and port 20 for transferring file data.