Clueless on how to use fsockopen() - php

I am very new to php, and i am trying to connect to ftp and post a form, which has a few text fields, and 3 image upload, the images will be uploaded to the server. I am using godaddy, and they dont allow ftp_connect, only fsocketopen(),and only available on ports 80 (http) and 443(https). Can i have some advice on how to approach this(fsockopen)?
I researched and below is what i got, i assume the first part is server, second part is the port so i assume is 80(as godaddy said only that 2 ports are available), but what are the last 3? The $error_number,$error_string, and the last part?
Thanks for your time. Sorry that if the question is a newbie question. I researched for a while, i still can't fix it.
fsockopen('abc.com', '80', $error_number, $error_string, 30)
<?php
$ftp_user_name='name';
$ftp_user_pass='pass';
$connection = 'server';
$errno='';
$connect= fsockopen("abc.info", 80, $errno, $errstr, 30) or die ("Cannot connect to host");
$login = ftp_login($connect, $ftp_user_name, $ftp_user_pass);
if (!$connect)
{die ("FTP connection has encountered an error!");}
//exit;
if (!$login)
{die ("But failed at login Attempted to connect to $connection for user $ftp_user_name....");}
?>

At the risk of sounding conceited, RTM please.
From the PHP Docs:
errno If provided, holds the system level error number that
occurred in the system-level connect() call. If the value returned in
errno is 0 and the function returned FALSE, it is an indication that
the error occurred before the connect() call. This is most likely due
to a problem initializing the socket.
errstr The error message as a string.
timeout The connection timeout, in seconds.

Related

PHP ftp_put returning "Unable to build data connection: Connection refused"

I have a PC that is running some FTP via PHP that I know used to work 1-2 months ago, but now I return to it I find that the PC is no longer working. I know I have been using the PC but I cannot think of what might have changed.
The PHP is throwing out error messages reading
Unable to build data connection: Connection refused
...when I use the ftp_put() function.
The cut down code I am using is:
<?php
$trackErrors = ini_get('track_errors');
ini_set('track_errors', 1);
$server="***.***.***.***";
$port=21;
echo "<LI>Connecting to $server:$port<BR>";
$conn_id = ftp_connect($server,$port,9999999) or die("<BR>Unable to connect to ".$server.":$port server.");
if ( !$conn_id ) {
$errmsg = $php_errormsg;
echo "<BR><LI>ERR:$errmsg";
}
else {
$passive=false;
echo "<LI>Setting Passive Mode=$passive";
ftp_pasv($conn_id, $passive);
$user="*********";
$pass="*********";
echo "<LI>Connecting as $user/*****";
if (!ftp_login($conn_id, $user, $pass)) {
$msg = "Failed to login to $selected_server as $user; <BR>check logincredentials in the Settings";
echo "<BR><LI>$msg";
$errmsg = $php_errormsg;
echo "<LI>ERR:$errmsg";
return $msg;
}
ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 10000);
if (!#ftp_put($conn_id, "test.txt", "C:......test.txt", FTP_BINARY)) {
echo "<BR><LI>ftp_put failed";
$errmsg = $php_errormsg;
echo "<LI>ERR:$errmsg";
}
echo "<HR>Done";
}
?>
the output when running this as a webpage is
Connecting to ***.***.***.***:21
Setting Passive Mode=
Connecting as *******/*****
ftp_put failed
ERR:ftp_put(): Unable to build data connection: Connection refused
Done
The result is that the ftp_put() gives the error message and leaves a zero (0) byte file with the right filename on the server.
The strange thing is is that
the same code/connection info works on another laptop ok
the same connection info works ok using FileZilla when pushing a file
the problem occurs on several servers (ie. it's not just one specific destination that has the problem)
Also, this doesn't seem to have anything to do with the passive mode (it fails with and without this enabled)
Does anyone have any suggestions?
Thanks
Abe
You are using the active FTP mode. In the active mode the server tries to connect to the client. In most network configurations, that's not possible as the client machine is usually behind a firewall.
That's why the server fails with:
Unable to build data connection: Connection refused
It's specifically ProFTPD error message for this situation.
See my article on the active and passive FTP connection modes for details.
The code can work on other machines, if they have firewall disabled or if they have rules that allow incoming traffic on unprivileged ports.
FileZilla works because it defaults to the passive mode (as most modern FTP clients do).
You have claimed to try the passive mode too, yet to get the same error message.
That's because you are using the ftp_pasv call incorrectly.
You have to move the ftp_pasv call after the ftp_login.
$user = "*********";
$pass = "*********";
echo "<LI>Connecting as $user/*****";
if (!ftp_login($conn_id, $user, $pass)) {
// ...
}
$passive = true;
echo "<LI>Setting Passive Mode=$passive";
ftp_pasv($conn_id, $passive);
The documentation clearly suggests it:
Please note that ftp_pasv() can only be called after a successful login or otherwise it will fail.
For a similar issue (just with Pure-FTPd), see PHP upload via FTP - ftp_put() I won't open a connection to x.x.x.x (only to y.y.y.y).

Warning: ftp_put(): Connecting to port

I cannot upload files into my ftp server.
There is always warning:
ftp_put(): Connecting to port.
<?php
set_time_limit(0);
$host = 'xxxx';
$usr = 'yyyy';
$pwd = 'zzzz';
$local_file = '/home/back.sql';
$ftp_path = '/public_html/';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
$upload = ftp_put($conn_id, $ftp_path.'back1.sql', $local_file, FTP_ASCII);
print($upload);
?>
The code were executed for three times. I got three different warnings.
Warning: ftp_put(): Connecting to port 1926 in filename (i named it) on line 10
Warning: ftp_put(): Connecting to port 1928 in filename (i named it) on line 10
Warning: ftp_put(): Connecting to port 1930 in filename (i named it) on line 10
What does the warning info mean?
Why connect to different ports? Maybe the ports should be 21 for every time, why not?
The "Connecting to port xxx" is a message issued by PureFTPD server, when it tries to connect back to the FTP client to its active mode data connection port (which is random, that's why it changes).
If you really need to use the active mode, you need to allow incoming connections to the active mode data connection port range used by PHP.
See my guide for network configuration necessary for the active mode FTP.
Though, if you do not need to use the active mode, use the passive mode instead. The passive mode generally does not require any network configuration on a client side.
In PHP, you switch to the passive mode by calling the ftp_pasv function after the ftp_login:
...
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
ftp_pasv($conn_id, true) or die("Cannot switch to passive mode");
...
See the above guide to understand the difference between the active and the passive FTP mode.

Connect to MySQLi through a specific IP

Due to a network outage, our architecture changed for a week or two. Now I am required to have 2 IPs in the network, one with 192.168.1.* and the second with 192.168.2.* .
The web development server where the mysql databases are located is accesible only from the 192.168.2.* IP.
Because of that, mySQLi connection takes exactly 20 seconds which is unacceptable :D
This is what I've tried so far:
$link = mysqli_init();
if (!mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 3)) {
die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
}
mysqli_real_connect($link, $_env[$ENVIRONMENT]['host'], $_env[$ENVIRONMENT]['user'], $_env[$ENVIRONMENT]['pass'], $_env[$ENVIRONMENT]['db'], 3306) or die("Could not connect: ".mysqli_error($link));
Setting a connect timeout of 3 seconds does not resolve the problem. How should I fix this?
(I use Windows)

php ftp connection timed out while working - reconnect? detect?

Is it possible to detect if the ftp resource is disconnected or timed out?
A sample script
<?php
$connection = ftp_connect('127.0.0.1');
ftp_login($connection, '123', '456');
sleep(660); // proftpd has 600 as default no transfer timeout
ftp_chdir($connection, '/');
ftp_close($connection);
Then we got a warning
Warning:
ftp_chdir():
No transfer timeout (600 seconds):
closing control connection in line 5
Is it possible to check if disconnected or timed out?
if(!$connection)
doesnt work because the resource exists...
But the resource is timed out :/
You can use this something like this
if(is_array(ftp_nlist($connection, "."))){
echo "Connected";
}
References:
http://php.net/manual/en/function.is-array.php
http://php.net/manual/en/function.ftp-nlist.php

sftp connection

When I' m trying to connect Sftp from my joomla site
its not connecting:
$c = ftp_connect('ftp.xyz.com') or die("Can't connect");
ftp_login($c, 'username' , 'pwd') or die("Can't login");
in this case msg showing Can't connect
I also tried this code
$connection = ssh2_connect('ftp.xyz.com', 22);
if (!$connection) die('Connection failed');
in this case no error msg showing
Please help me, if there is a proper solution help me please.
Thanks
ftp_connect() uses FTP, not SFTP. They're very different. So if your host is providing only SFTP, then no, that function won't work! SFTP is explained in more detail here:
http://en.wikipedia.org/wiki/SSH_File_Transfer_Protocol
ssh2_connect() is the right connection method to use, which is why it's probably working. You can see all the SSH2 functions available in PHP here:
http://php.net/manual/en/ref.ssh2.php
You'll probably be most interested in ssh2_scp_recv() and ssh2_scp_send() (for getting and sending files).

Categories