Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-14-2013, 03:11 AM   PM User | #1
Remix919
Regular Coder

 
Join Date: Jan 2006
Posts: 193
Thanks: 29
Thanked 0 Times in 0 Posts
Remix919 is an unknown quantity at this point
PHP FTP Transfer Issue

I'm having problems with a simple PHP FTP script, it partially works, it connects fine and creates the file on the new FTP account, but I get a warning error and the file is empty.

Warning: ftp_put() [function.ftp-put]: PORT command successful in /home/export.php on line 16

Any idea what I'm doing wrong?

PHP Code:

$file 
"filefolder/csvfile.txt"/*source file on the server which we wana download ,single file name refers that file is in Home/root*/
$local_file "newfolder/csvfile.txt";//download file and store as local.tar
//================================
$sessid ftp_connect($server); //connect
$login_ok ftp_login($sessid$user"$pass"); //login
if ((!$sessid) || (!$login_ok)):
 echo 
"failed to connect: check hostname, username & password";
 exit; 
//failed? Unable to connect!
endif;
 
if (
ftp_put($sessid$local_file$fileFTP_BINARY)) //Ftp get function which will download file
{
 echo 
"Successfully written to $local_file\n";
} else {
 echo 
"There was a problem\n";
}
 
ftp_close($sessid); 
Remix919 is offline   Reply With Quote
Old 01-14-2013, 03:22 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
$file is a string. It needs to be a resource. Run the file through an fopen and provide the third argument with that file resource. The variable names may be incorrect though; you are running a put call, but you are calling the file you are uploading as $file and the remote name will be $local_file. These seem backwards to me.

If you still have issues, try running a ftp_pasv($sessid, true); first to run in passive mode.
Fou-Lu is offline   Reply With Quote
Old 01-14-2013, 03:30 AM   PM User | #3
Remix919
Regular Coder

 
Join Date: Jan 2006
Posts: 193
Thanks: 29
Thanked 0 Times in 0 Posts
Remix919 is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
$file is a string. It needs to be a resource. Run the file through an fopen and provide the third argument with that file resource. The variable names may be incorrect though; you are running a put call, but you are calling the file you are uploading as $file and the remote name will be $local_file. These seem backwards to me.

If you still have issues, try running a ftp_pasv($sessid, true); first to run in passive mode.
I tried that but if I do that I get:

Warning: ftp_put() expects parameter 3 to be string, resource given

And yea, the names are a bit backwards, I had taken this script from one that originally used the ftp_get function, but I just changed it to put instead.

Last edited by Remix919; 01-14-2013 at 03:37 AM..
Remix919 is offline   Reply With Quote
Old 01-14-2013, 03:41 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Oh yeah my bad you're right. I was thinking of ftp_fput, not ftp_put.
Open the passive mode. I'm not that familiar with the possible errors from the ftp. This is definitely bizarre looking to me (it looks like it successfully did nothing :/).
Make sure your filepath is valid as well for the local file. You can check that using file_exists. It'll need to be readable as well. Pull a list of the ftp_nlist as well to see if the directory for the file you want to upload is available. The documentation doesn't specify, but I don't think FTP allows you to create a directory implicitly with an upload.
Fou-Lu is offline   Reply With Quote
Old 01-14-2013, 03:48 AM   PM User | #5
Remix919
Regular Coder

 
Join Date: Jan 2006
Posts: 193
Thanks: 29
Thanked 0 Times in 0 Posts
Remix919 is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
Oh yeah my bad you're right. I was thinking of ftp_fput, not ftp_put.
Open the passive mode. I'm not that familiar with the possible errors from the ftp. This is definitely bizarre looking to me (it looks like it successfully did nothing :/).
Make sure your filepath is valid as well for the local file. You can check that using file_exists. It'll need to be readable as well. Pull a list of the ftp_nlist as well to see if the directory for the file you want to upload is available. The documentation doesn't specify, but I don't think FTP allows you to create a directory implicitly with an upload.
What's the difference between fput and put? I think all the paths are correct because it successfully creates the file in the right directory, the problem is the file is empty :\ Tried the passive mode and same issue, warning message and just puts an empty file there.
Remix919 is offline   Reply With Quote
Old 01-14-2013, 04:27 AM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I believe it will always attempt to create the file and then take the data within the source to copy. If the source isn't valid, it would result in an empty file.
fput is using an open file, put is using a filename. I would use the fput since you can verify with 100% certainty that the file is readable after opening before moving it to the server.
Fou-Lu is offline   Reply With Quote
Old 01-14-2013, 04:34 AM   PM User | #7
Remix919
Regular Coder

 
Join Date: Jan 2006
Posts: 193
Thanks: 29
Thanked 0 Times in 0 Posts
Remix919 is an unknown quantity at this point
I think I found my problem:

http://www.elitehosts.com/blog/php-f...nat-nightmare/
Remix919 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:09 PM.


Advertisement
Log in to turn off these ads.