LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices


Reply
  Search this Thread
Old 10-01-2009, 05:09 AM   #1
Orayi
LQ Newbie
 
Registered: Sep 2009
Posts: 6

Rep: Reputation: 0
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();

Any help would be greatly appriciated.

Kind regards,

Nick

PHP Code:
$ftp_server '1.2.3.4';
$ftp_user_name 'user';
$ftp_user_pass 'pass';
$ftp_port 21;

// 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";
    }

$destination_file 'robots.txt';
$source_file 'robots.txt';

// upload the file
$upload ftp_put($conn_id$destination_file$source_fileFTP_BINARY);

// check upload status
if (!$upload) {
        echo 
"FTP upload has failed!";
    } else {
        echo 
"Uploaded $source_file to $ftp_server as $destination_file";
    }

// close the FTP stream
ftp_close($conn_id); 
 
Old 10-01-2009, 10:07 PM   #2
reickler
LQ Newbie
 
Registered: Sep 2009
Posts: 29

Rep: Reputation: 20
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:

if ((!$conn_id) || (!$login_result)) {
 
Old 10-02-2009, 03:56 AM   #3
Orayi
LQ Newbie
 
Registered: Sep 2009
Posts: 6

Original Poster
Rep: Reputation: 0
If I remove the "or die" then you get the error:

When running from a webpage:
"Warning: ftp_login() expects parameter 1 to be resource, boolean given in /var/www/ftp_test.php on line 28"

var_dump($conn_id); returns bool(false).

From command line /usr/bin/php /var/www/ftp_test.php

var_dump($conn_id); returns resource(4) of type (FTP Buffer).

Script completes.
 
Old 10-02-2009, 12:28 PM   #4
reickler
LQ Newbie
 
Registered: Sep 2009
Posts: 29

Rep: Reputation: 20
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).

su apache -c 'php foo.php'
 
Old 10-03-2009, 08:03 AM   #5
Orayi
LQ Newbie
 
Registered: Sep 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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.

Configure Command are quite different:

Apache
Code:
'./configure' '--host=x86_64-redhat-linux-gnu' '--build=x86_64-redhat-linux-gnu' '--target=x86_64-redhat-linux' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib64' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/usr/com' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--cache-file=../config.cache' '--with-libdir=lib64' '--with-config-file-path=/etc' '--with-config-file-scan-dir=/etc/php.d' '--disable-debug' '--with-pic' '--disable-rpath' '--without-pear' '--with-bz2' '--with-curl' '--with-exec-dir=/usr/bin' '--with-freetype-dir=/usr' '--with-png-dir=/usr' '--enable-gd-native-ttf' '--without-gdbm' '--with-gettext' '--with-gmp' '--with-iconv' '--with-jpeg-dir=/usr' '--with-openssl' '--with-pcre-regex=/usr' '--with-zlib' '--with-layout=GNU' '--enable-exif' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-sysvmsg' '--enable-wddx' '--with-kerberos' '--enable-ucd-snmp-hack' '--enable-shmop' '--enable-calendar' '--without-mime-magic' '--without-sqlite' '--with-libxml-dir=/usr' '--enable-xml' '--with-system-tzdata' '--with-apxs2=/usr/sbin/apxs' '--without-mysql' '--without-gd' '--disable-dom' '--disable-dba' '--without-unixODBC' '--disable-pdo' '--disable-xmlreader' '--disable-xmlwriter' '--disable-json' '--without-pspell'
CLI
Code:
'./configure'  '--host=x86_64-redhat-linux-gnu' '--build=x86_64-redhat-linux-gnu' '--target=x86_64-redhat-linux' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib64' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/usr/com' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--cache-file=../config.cache' '--with-libdir=lib64' '--with-config-file-path=/etc' '--with-config-file-scan-dir=/etc/php.d' '--disable-debug' '--with-pic' '--disable-rpath' '--without-pear' '--with-bz2' '--with-curl' '--with-exec-dir=/usr/bin' '--with-freetype-dir=/usr' '--with-png-dir=/usr' '--enable-gd-native-ttf' '--without-gdbm' '--with-gettext' '--with-gmp' '--with-iconv' '--with-jpeg-dir=/usr' '--with-openssl' '--with-pcre-regex=/usr' '--with-zlib' '--with-layout=GNU' '--enable-exif' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-sysvmsg' '--enable-wddx' '--with-kerberos' '--enable-ucd-snmp-hack' '--enable-shmop' '--enable-calendar' '--without-mime-magic' '--without-sqlite' '--with-libxml-dir=/usr' '--enable-xml' '--with-system-tzdata' '--enable-force-cgi-redirect' '--enable-pcntl' '--with-imap=shared' '--with-imap-ssl' '--enable-mbstring=shared' '--enable-mbregex' '--with-ncurses=shared' '--with-gd=shared' '--enable-bcmath=shared' '--enable-dba=shared' '--with-db4=/usr' '--with-xmlrpc=shared' '--with-ldap=shared' '--with-ldap-sasl' '--with-mysql=shared,/usr' '--with-mysqli=shared,/usr/bin/mysql_config' '--enable-dom=shared' '--with-pgsql=shared' '--with-snmp=shared,/usr' '--enable-soap=shared' '--with-xsl=shared,/usr' '--enable-xmlreader=shared' '--enable-xmlwriter=shared' '--enable-fastcgi' '--enable-pdo=shared' '--with-pdo-odbc=shared,unixODBC,/usr' '--with-pdo-mysql=shared,/usr' '--with-pdo-pgsql=shared,/usr' '--with-pdo-sqlite=shared,/usr' '--with-pdo-dblib=shared,/usr' '--enable-json=shared' '--enable-zip=shared' '--with-readline' '--enable-dbase=shared' '--with-pspell=shared' '--with-mcrypt=shared,/usr' '--with-mhash=shared,/usr' '--with-tidy=shared,/usr' '--with-mssql=shared,/usr' '--with-unixODBC=shared,/usr'
 
Old 10-03-2009, 08:59 AM   #6
Orayi
LQ Newbie
 
Registered: Sep 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Turned off selinux and now it works.

Thanks so much for pointing me towards that.
 
Old 11-25-2009, 06:01 PM   #7
hubschrauber
LQ Newbie
 
Registered: Nov 2009
Posts: 1

Rep: Reputation: 0
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
 
Old 12-22-2009, 10:06 AM   #8
jcorbit
LQ Newbie
 
Registered: Dec 2009
Posts: 1

Rep: Reputation: 0
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!
 
Old 09-28-2018, 08:58 AM   #9
congloi
LQ Newbie
 
Registered: Sep 2018
Posts: 1

Rep: Reputation: Disabled
Talking

Quote:
Originally Posted by Orayi View Post
Turned off selinux and now it works.

Thanks so much for pointing me towards that.
this post was 9 years ago, but I want to say thank you.

same problem, lost more 6 hours to find the solution.

only simple disable selinux (fuk selinux )

thank you again
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Not Working - CentOS 4.6 w/ Virtualmin pbeane Linux - Server 1 02-20-2008 09:59 PM
Apache PHP != Command Line PHP? wh33t Linux - Server 3 10-23-2007 09:21 PM
PHP on the command line? kev000 Linux - General 4 11-28-2006 03:16 PM
Command line/tools in CentOS/RHEL ? tuxuser19 Red Hat 5 10-01-2006 07:07 PM
php from command line odious1 Linux - Software 4 05-03-2004 12:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat

All times are GMT -5. The time now is 02:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration