CentOS 5.3 - PHP - ftp_connect working command line only
Red HatThis forum is for the discussion of Red Hat Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
CentOS 5.3 - PHP - ftp_connect working command line only
Hi,
I've been having a bit of a strange problem with the PHP function ftp_connect and after 2 days of search google / trying to fix it I thought I'd see if anyone else had this or might be able to fix it.
I'm using the example script from the php site. I've pasted the code below. The problem I'm getting is that ftp_connect always returns false with the only exception being if I break the variable $ftp_server, where I get a name lookup error instead.
The script works exactly as intended when running it from a command line /usr/bin/php ftp_test.php which leads me to beleive that this is either a permissions problem or apache.
Using telnet with the same port and address also opens an FTP connection.
I've also tried changing the owner to apache and root and permission to 0777 so I think that rules out it being a permission issue?
I'm using PHP 5.2.11 but downgraded to 5.1.6 last night and still had the same problem. It also works fine on another virtual machine on our server running Ubuntu.
php -m shows ftp listed as one of the modules and it's also enabled on phpinfo();
// set up basic connection
$conn_id = ftp_connect($ftp_server,$ftp_port) or die("Could not connect to $ftp_server:$ftp_port");
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
Nothing stands out as being incorrect (but I'm not a php guru), but one suggestion that I'd make is to add some basic debugging (just a series of print statements would suffice).
For example:
// set up basic connection
$conn_id = ftp_connect($ftp_server,$ftp_port) or die("Could not connect to $ftp_server:$ftp_port");
// Add this
print $conn_id;
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// Add this
print $login_result;
Adding those simple print statements will help narrow down where exactly the script is failing.
Lastly, while it won't solve your problem, I'd suggest breaking this bit out into two separate if statements, so when you actually do get your error handling working, you can differentiate between a failed connection and a failed login:
Interesting problem. I know you said you check phpinfo already, but have you tried comparing both the command line and webserver output from phpinfo to see if they show the same thing (i.e. versions, modules, etc)? Could be that you're using a different version through the web than you're using on the command line?
You should also try running the script from the command line as the user that your webserver is running as (assuming the user has shell access) to see if it's maybe permissions (I know you said you checked ownership and mode, but it could be selinux or the likes).
I gave shell access to apache and it runs the script fine using the command line version.
Having looked at the phpinfo files they both seem to be running the same modules but it's hard to tell as one outputs html and the other text so I can't use beyond compare.
If you don't want to turn off selinux completely, you might get what you need by just setting the httpd_can_network_connect using the setsebool command.
Verify that it was previously set to "off": getsebool httpd_can_network_connect
Set it to "on": setsebool httpd_can_network_connect=1
Turn selinux back on: setenforce 1
Check to be sure php ftp_connect still works when running under httpd.
Set the policy (-P) to "on" so it persists over a reboot: setsebool -P httpd_can_network_connect=1
I know this was answered for the original poster, but I am having a very similar problem running Ubuntu (Hardy) (selinux not installed). I can connect to an outside FTP server using linux FTP, but cannot connect using PHP ftp_connect either from CLI or from browser (always fails on connection). I've tried this on two separate Ubuntu boxes and the results are the same (one version 9.04 and one version 8.04) . Looking at tcpdump indicates that I'm sending/receiving data to the target server, but php still fails to connect.
Any ideas as to how I can debug this problem would be greatly appreciated!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.