Page 2 of 3
Re: Uploading file into ftp server
Posted: Sat Jul 23, 2011 2:16 pm
by YoussefSiblini
I am in my local server and I want to transfer the excel file which has the bath xls/AB-MXRPayment.xls to only one server which is the server B (server A was a test server).
So are you saying that I am not able to connect to the server B port 21 because I am already in a port 21 server which is my local server? if yes, Do I need to change my local server port to 20 in the code or some where in the hosting C-panel.
<?php
//Send the FTP Connection and send the Excel file to the ftp server
$ftp_server = "xxxx";
$ftp_user = "xxx";
$ftp_pass = "xxxx";
$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";
}
ftp_pasv ($conn_id, true);
if (@ftp_fput($conn_id, $fp, $file, FTP_BINARY)) {
echo "Successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
I can give you the ftp details for server B if you want to give it a try to connect it using port 21 (I am bored tell death from trying to connect it into port 21).
Youssef
Re: Uploading file into ftp server
Posted: Sat Jul 23, 2011 7:08 pm
by McInfo
Okay, I was thinking you were connecting to A and B simultaneously. I'm straightened out now, I think. Forget my last suggestion. (Off topic: Simultaneous FTP connections shouldn't cause problems, anyway. I have tested and confirmed that PHP can be connected to two FTP servers simultaneously.)
What I meant by FTP using ports 21 and 20 is that two separate connections are created when you connect to an FTP server. (I've been reviewing this material in my networking basics book.) The initial connection is made to port 21 so the client can tell the server what to do, and a behind-the-scenes connection is made to port 20 for the actual data transfer. I think it's unusual that you can initialize the connection on port 20. When I try to connect to my FTP servers on port 20, ftp_connect() times out.
Are you able to connect and transfer files to the FTP server using a desktop FTP program?
Sharing credentials is not something I would do, but if you feel comfortable doing it, you can send them to me in a private message; and I will try to connect.
Re: Uploading file into ftp server
Posted: Sun Jul 24, 2011 3:57 am
by YoussefSiblini
I tried and it didn't work, I think it is not possible to send file to an ftp port 20 server.
Can you give me your email so I can send you the ftp details, I can change the ftp details later
Youssef
Re: Uploading file into ftp server
Posted: Sun Jul 24, 2011 3:58 am
by YoussefSiblini
Or shall I send them to you in a private message?
Re: Uploading file into ftp server
Posted: Sun Jul 24, 2011 4:51 am
by YoussefSiblini
Hi McInfo,
I have a good news, I tried to upload an image from my desktop (my local machine and not from the hosted company server) and it worked all what I did is changed the path of my local file into the desktop image path here is the code I used:
<?php
//Send the FTP Connection and send the Excel file to the ftp server
$ftp_server = "xxxx";
$ftp_user = "xxx";
$ftp_pass = "xxxx";
$file = 'file:///C:/Users/Youssef/Desktop/logo.png';
$fp = '/hello.png';
// 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";
}
ftp_pasv ($conn_id, true);
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);
?>
So we are in a point now where we know that the local file in the problem, Do I need to do some thing to the file before I send it to the server B?
Re: Uploading file into ftp server
Posted: Sun Jul 24, 2011 12:43 pm
by McInfo
YoussefSiblini wrote:Or shall I send them to you in a private message?
That is what I suggested. However, I am not sure that it is necessary now that you successfully uploaded the image file. The problem appears to be related to how you are accessing the file to be uploaded, not the connection itself.
YoussefSiblini wrote:Do I need to do some thing to the file before I send it to the server B?
Check that the file to be uploaded exists (
file_exists() or
is_readable()) before calling ftp_put(). Actually, if the only purpose of the FTP connection is to upload the file, do the check before ftp_connect().
Are you able to upload your image if you remove the file protocol ("file://") from the path?
Code: Select all
$file = 'C:/Users/Youssef/Desktop/logo.png'; // or
//$file = 'C:\Users\Youssef\Desktop\logo.png';
Re: Uploading file into ftp server
Posted: Sun Jul 24, 2011 1:17 pm
by YoussefSiblini
The file exists and readable in $file = 'xls/AB-MXRPayment.xls';
The file will upload normally if I removed the file:// from the path.
Re: Uploading file into ftp server
Posted: Sun Jul 24, 2011 1:34 pm
by McInfo
Is that XLS file the only file that fails? Do other XLS files fail?
Re: Uploading file into ftp server
Posted: Sun Jul 24, 2011 2:00 pm
by YoussefSiblini
I tried others and they do the same.
If I use ftp_put($conn_id, $fp, $file , FTP_ASCII) I get this:
The file is readable and exists
Connected as user1@80.45.12.74 xls/AB-MXRPayment.xls
Warning: ftp_put() [function.ftp-put]: php_connect_nonb() failed: Operation now in progress (115) in /home/abmxrcom/public_html/ipn.php on line 197
Warning: ftp_put() [function.ftp-put]: Type set to A in /home/abmxrcom/public_html/ipn.php on line 197
There was a problem while uploading xls/AB-MXRPayment.xls
And if I use ftp_fput($conn_id, $fp, $file , FTP_ASCII) I get
The file is readable and exists
Connected as user1@80.45.12.74 xls/AB-MXRPayment.xls
Warning: ftp_fput() expects parameter 3 to be resource, string given in /home/abmxrcom/public_html/ipn.php on line 197
There was a problem while uploading xls/AB-MXRPayment.xls
Re: Uploading file into ftp server
Posted: Sun Jul 24, 2011 2:21 pm
by McInfo
What other types of files have you tried? Do they fail?
If you compress the XLS into a Zip file, does the Zip upload?
Have you tried replacing FTP_ASCII with FTP_BINARY?
The ftp_fput() function uses a file resource, not a file path string. A file resource is created by fopen().
Re: Uploading file into ftp server
Posted: Sun Jul 24, 2011 2:40 pm
by YoussefSiblini
I tried another excel file, php file, html file and an png image. all of them act the same.
I tried zipping the file it is not working.
I have tried replacing FTP_BINARY and getting the same error.
But now when I used the fopen() I get error in parameter 2 and not 3, this is what I get:
The file is readable and exists
File opened
Connected as user1@80.45.12.74 xls/hello.htm
Warning: ftp_fput() expects parameter 2 to be string, resource given in /home/abmxrcom/public_html/ipn.php on line 206
There was a problem while uploading xls/hello.htm
Does that mean that parameter 3 is sorted out?
Re: Uploading file into ftp server
Posted: Sun Jul 24, 2011 2:48 pm
by YoussefSiblini
But now when I used the fopen() I get error in parameter 2 and not 3, this is what I get:
The file is readable and exists
File opened
Connected as user1@80.45.12.74 xls/hello.htm
Warning: ftp_fput() expects parameter 2 to be string, resource given in /home/abmxrcom/public_html/ipn.php on line 206
There was a problem while uploading xls/hello.htm
Does that mean that parameter 3 is sorted out?
Sorry Ignore these top comments I used the same variable for the remote file and the fopen() , now I fixed it and I am back to the parameter 3 error.
Re: Uploading file into ftp server
Posted: Sun Jul 24, 2011 2:58 pm
by McInfo
YoussefSiblini wrote:I tried another excel file, php file, html file and an png image. all of them act the same.
Before, you said you were able to upload logo.png. What changed?
YoussefSiblini wrote:But now when I used the fopen() I get error in parameter 2 and not 3,
Only the local file (parameter 3) should be a resource. The remote file path (parameter 2) should be a string.
Re: Uploading file into ftp server
Posted: Sun Jul 24, 2011 3:22 pm
by McInfo
You mentioned cPanel. Are you able to change the ports?
Re: Uploading file into ftp server
Posted: Sun Jul 24, 2011 3:25 pm
by YoussefSiblini
I can change the port in my local server (the one I am sending the file from) and not the server where I am uploading the file into (server B).