Uploading file into ftp server
Moderator: General Moderators
-
YoussefSiblini
- Forum Contributor
- Posts: 206
- Joined: Thu Jul 21, 2011 1:51 pm
Re: Uploading file into ftp server
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
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
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.
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.
-
YoussefSiblini
- Forum Contributor
- Posts: 206
- Joined: Thu Jul 21, 2011 1:51 pm
Re: Uploading file into ftp server
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
Can you give me your email so I can send you the ftp details, I can change the ftp details later
Youssef
-
YoussefSiblini
- Forum Contributor
- Posts: 206
- Joined: Thu Jul 21, 2011 1:51 pm
Re: Uploading file into ftp server
Or shall I send them to you in a private message?
-
YoussefSiblini
- Forum Contributor
- Posts: 206
- Joined: Thu Jul 21, 2011 1:51 pm
Re: Uploading file into ftp server
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?
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
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:Or shall I send them to you in a private message?
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().YoussefSiblini wrote:Do I need to do some thing to the file before I send it to the server B?
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';-
YoussefSiblini
- Forum Contributor
- Posts: 206
- Joined: Thu Jul 21, 2011 1:51 pm
Re: Uploading file into ftp server
The file exists and readable in $file = 'xls/AB-MXRPayment.xls';
The file will upload normally if I removed the file:// from the path.
The file will upload normally if I removed the file:// from the path.
Re: Uploading file into ftp server
Is that XLS file the only file that fails? Do other XLS files fail?
-
YoussefSiblini
- Forum Contributor
- Posts: 206
- Joined: Thu Jul 21, 2011 1:51 pm
Re: Uploading file into ftp server
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
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
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().
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().
-
YoussefSiblini
- Forum Contributor
- Posts: 206
- Joined: Thu Jul 21, 2011 1:51 pm
Re: Uploading file into ftp server
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?
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?
-
YoussefSiblini
- Forum Contributor
- Posts: 206
- Joined: Thu Jul 21, 2011 1:51 pm
Re: Uploading file into ftp server
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.
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
Before, you said you were able to upload logo.png. What changed?YoussefSiblini wrote:I tried another excel file, php file, html file and an png image. all of them act the same.
Only the local file (parameter 3) should be a resource. The remote file path (parameter 2) should be a string.YoussefSiblini wrote:But now when I used the fopen() I get error in parameter 2 and not 3,
Re: Uploading file into ftp server
You mentioned cPanel. Are you able to change the ports?
-
YoussefSiblini
- Forum Contributor
- Posts: 206
- Joined: Thu Jul 21, 2011 1:51 pm
Re: Uploading file into ftp server
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).
Last edited by YoussefSiblini on Sun Jul 24, 2011 3:31 pm, edited 1 time in total.