/    Sign up×
Community /Pin to ProfileBookmark

PHP FTP… what am I doing wrong?

Are there some sort of server configurations that have to be set up for this to work? Are did I just screw up the code? (I switched out the actual ftp server / username / password info.)

[CODE]
<?php
echo “test”;

$ftp_server = “ftpserver”;
$conn_id = ftp_connect($ftp_server) or die(“nope.”);

echo “test”;

$ftp_user_name = “username”;
$ftp_user_pass = “password”;

$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die(“nope.”);

echo “test”;
?>
[/CODE]

I don’t get any of the “or die” responses nor do I get any of the “test” echos besides the first one.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@svidgenJun 18.2009 — First, check your logs, presuming this is being run via apache/iis.

Second, make sure you can actually FTP into the destination from the box this script is running on.

Third, try giving it an N-second timeout:
[code=php]// same deal, but with 5-second timeout
$conn_id = ftp_connect($ftp_server, 21, 5) or die("nope."); [/code]

... See what happens.

Otherwise your code looks fine ...
Copy linkTweet thisAlerts:
@xvszeroauthorJun 18.2009 — This might seem like an ignorant question, but if I couldn't FTP into it wouldn't it return the "or die" statement?

Even when I put the 5 second timeout the page ends instantly.
Copy linkTweet thisAlerts:
@svidgenJun 18.2009 — This might seem like an ignorant question, but if I couldn't FTP into it wouldn't it return the "or die" statement?[/QUOTE]
Once it realizes it can't connect, yes, it should. But, if PHP or Apache time out before the function does, you'll see the page return with no die() message.

Check your apache error logs. And of course, if you don't see anything, throw this at the top of the script to be absolutely sure you're seeing all errors:
[code=php]error_reporting(E_ALL);[/code]
Copy linkTweet thisAlerts:
@JunkMaleJun 19.2009 — Your FTP login under PHP is not a persistant connection, so if nothing is to be sent, the FTP connection is severed, so unless your actually going to "UPLOAD" something, your not going to know if its a success.

I has similar problems when making an FTP widget and found on my travels that FTP does not work the same as regular FTP, each time your upload session ceases, your FTP and login is killed off. I presume that this is to free up server resources.

So lines like :

$conn_id = ftp_connect($ftp_server) or die("nope.");

are better off nested in an if () conditional statement block
[code=php]if( $conn_id = ftp_connect($ftp_server) ){
if( $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) ){
// we get here, were logged in...
echo "We logged in OK!";
// lets now upload and do stuff!!!
}else{
die("Login failure: username and / or password not correct");
}
}else{
die("Connection faliure: cannot connect to {$ftp_server}");
}[/code]


Your ftpserver name is important, it is always better to use a direct IP address, this saves on resolving the DNS to an IP address which in itself can cause issues if the host you use does not allow external connections or domain name resolving requests. Some hosts allow things that others do not. It may pay you to ask a "Tech" question to the "Tech" department of your host.

By changing the way the script functions, you will get a better idea of where the error is.
Copy linkTweet thisAlerts:
@xvszeroauthorJun 19.2009 — ^^^ Hmm, but if it severs, wouldn't it theoretically get to my second "test" echo? It never gets there, so I'm assuming something died.

I think I am the tech department of my host. Heh. Very, very small IT department here and we host all of our own stuff. Pretty much I could talk to my boss about it and he may or may not have any idea. He likes when we figure things out on our own though. Often requires it.
Copy linkTweet thisAlerts:
@JunkMaleJun 20.2009 — "echo test" you mean...?

Why would you want that? The thing about FTP is that you have to run the whole lot in one go, you can not "Test" a connection without the rest of the commands like getting the current working directory and changing directory to move in to a directory or out of a directory and eventually upload.

It all has to be a sequence of instructions that follow each other as the process needs to be issued in one command. As I have said, it is not like regular FTP so don't expect it to function like an FTP client, it won't.
Copy linkTweet thisAlerts:
@chazzyJun 20.2009 — "echo test" you mean...?

Why would you want that? The thing about FTP is that you have to run the whole lot in one go, you can not "Test" a connection without the rest of the commands like getting the current working directory and changing directory to move in to a directory or out of a directory and eventually upload.

It all has to be a sequence of instructions that follow each other as the process needs to be issued in one command. As I have said, it is not like regular FTP so don't expect it to function like an FTP client, it won't.[/QUOTE]


I think the behavior you're describing is platform dependent behavior of FTP. we have PHP FTP based scripts running, and they don't require some large if block wrapping each action. if you look at the basic example on the PHP website, you'll see what I mean. http://us3.php.net/manual/en/ftp.examples-basic.php

As for why die might not work, is it possible that you're behind a firewall?
Copy linkTweet thisAlerts:
@JunkMaleJun 21.2009 — That will depend on how your connection is made.

This is something I read up on-line when I was attempting to use PHP to connect from my local machine install to the website via FTP. If you have a persistant connection, I would think that this is because the system your company has is using sockets.
×

Success!

Help @xvszero spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...