PHP times out when retrieving items - php

I'm trying to get a directory listing from a FTP server which only accepts FTP connections over explicit TLS.
<?php
$ftp_server = "...";
$ftp_user_name = "...";
$ftp_user_pass = "...";
// set up basic ssl connection
$conn_id = ftp_ssl_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
echo $login_result . "<br/>";
// change dir to "data"
$chdir_result = ftp_chdir($conn_id, "data");
echo $chdir_result . "<br/>";
echo ftp_pwd($conn_id) . "<br/>";
// get contents of the current directory
$contents = ftp_rawlist($conn_id, ".");
// output $contents
var_dump($contents);
// close the ssl connection
ftp_close($conn_id);
?>
The script hangs for about a minute and gives the following output:
1
1
/data
bool(false)
I also tried using ftp_nlist and print_r with no avail. (print_r just prints blank).

If you have a firewall or NAT enabled on your server then you have to use passive mode for your ftp connection
Add this before ftp_rawlist()
ftp_pasv( $conn_id, true );

Related

ftp_get() working on localhost but not on live server

<?php
error_reporting(E_ALL);
set_time_limit(300);//for setting
$path='/PickUpOld';
$ftp_server='xxxxxx.com';
$ftp_server_port="xx";
$ftp_user_name='xxxxxxx';
$ftp_user_pass="xxxxxxxx";
// set up a connection to ftp server
$conn_id = ftp_connect($ftp_server, $ftp_server_port);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$ps = ftp_pasv($conn_id, true);
// check connection and login result
if ((!$conn_id) || (!$ps)) {
echo "Fail</br>";
} else {
echo "Success</br>";
// enabling passive mode
ftp_pasv( $conn_id, true );
// get contents of the current directory
$contents = ftp_nlist($conn_id, $path);
// output $contents
$local_file = 'rana.php';
$server_file = 'PickUpOld/test.php';
if ( ftp_get( $conn_id, $local_file, $server_file, FTP_BINARY ) ) {
echo "WOOT! Successfully written to $local_file\n";
}
else {
echo "Doh! There was a problem\n";
}
}
// close the FTP connection
ftp_close($conn_id);
?>
I am trying to copy files one server to another. This Script working fine on localhost, but not working on Live server.
Please share your thoughts where i am doing wrong.
Thanks,

Unable to connect to server using ftp_connect

I've tried just about everything, but unable to form a connection using ftp_connect. There is no error message, so I don't know where to look. It just times-out.
Here is the code:
<?php
$remote_file = 'file.txt';
$ftp_host = '1.1.1.1';
$ftp_user_name = 'root#1.1.1.1';
$ftp_user_pass = 'password';
$local_file = 'file.txt';
$connect_it = ftp_connect($ftp_host) or die("Could not connect");
$login_result = ftp_login($connect_it, $ftp_user_name, $ftp_user_pass);
if ( ftp_put( $connect_it, $remote_file, $local_file, FTP_BINARY ) ) {
echo "WOOT! Successfully transfer $local_file\n";
}
else {
echo "Doh! There was a problem\n";
}
ftp_close( $connect_it );
?>
Both servers are mine so I know they are both on and working. The server is trying to connect and unless I set up a time out it just keeps going and going but the connection is not made. All I'm trying to do is transfer one text file from one VPS to another.
If your connection simply times out without error, this means there is a port that isn't opened on one server or the other.
Here is what you could try:
maybe turn error reporting on:
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
check on both servers that the port you intend to use is opened
$ netstat -a|grep ftp
tcp 0 0 *:ftp *:* LISTEN
(here you try to use the default that should be "21")
check that you're able to telnet the thing
telnet <host> <port>
Then try just the minimal ftp connect
$ftp_host='1.1.1.1';
$ftp_user_name = 'root'; // I think in your case you should not add '#1.1.1.1'...
$ftp_user_pass = 'password';
$conn_id = ftp_connect($ftp_host) or die("Couldn't connect to $ftp_host");
// Then chdir to a directory:
ftp_chdir($conn_id, "/your/dir");
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
See the full documentation of PHP's FTP functions.

Cannot connect to ftp through PHP

I have migrated my code from xampp to lamp recently. Since that time I have a problem with ftp_connect function and it always returns false. Here is the code:
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
Is there any setting in PHP or apache I have to set in advanced!?
This example works for me:
$ftp_server = "SERVER IP";
$conn_id = ftp_connect($ftp_server);
$ftp_user_name = "YOUR USERNAME";
$ftp_user_pass = "YOUR PASSWORD";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$contents = ftp_nlist($conn_id, '/');
for ($i = 0 ; $i < count($contents) ; $i++)
echo "<li>" . substr($contents[$i],1) . "</li>";
ftp_close($conn_id);
Try running this:
<?php
$c = ftp_connect('ftp.mozilla.org');
var_dump($c);
$c = ftp_connect('abcdefg');
var_dump($c);
?>
You should get this:
resource(2) of type (FTP Buffer) Warning: ftp_connect()
[function.ftp-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\abc\def.php on line 5
bool(false)
Answer on : How to get error if FTP server is invalid.?. Then you will know what kind of error appears.

PHP ftp_nlist() returning bool(false) even with passive mode and different users

I'm currently making a script that will backup files that I have on my server but am having a major issue. About 30 minutes ago I was able to connect using the same script, list my files, and download them. But now when I try to connect, it connects and that is it, I can't ftp_nlist the files because when I do it just returns bool(false). I read on some other posts that I should try setting passive mode to true with ftp_pasv but it did not change anything. I've also tried other users which do the same exact thing. I tried using the credentials on filezilla and it connected and allowed me to download with no issues. Any help would be greatly appreciated, I'll post the code below.
PHP:
Connect to FTP
$ftp_server = "Hidden for security";
$ftp_user = "Hidden for security";
$ftp_pass = "Hidden for security";
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
ftp_pasv($conn_id, true);
Get filenames on FTP
$contents = ftp_nlist($conn_id, ".");
$rm = array(0, 1, 2, 3,);
foreach($rm as $value) {
unset($contents[$value]);
}
$contents = array_values($contents);
Get filenames on localhost
$dir = scandir("images/");
$rmval = array(0, 1, 2);
foreach ($rmval as $val) {
unset($dir[$val]);
}
$dir = array_values($dir);
Try to login
if (ftp_login($conn_id, $ftp_user, $ftp_pass)) {
//echo "Connected";
$counter = 0;
var_dump($contents);
/*foreach ($ftp_content as $file) {
ftp_get($conn_id, "images/" . $ftp_content[$counter], $ftp_content[$counter], FTP_BINARY);
$counter++;
}*/
} else {
echo "Couldn't Connect";
}
Close FTP connection
ftp_close($conn_id);
The issue was that I was trying to list my files before establishing a connection to the FTP server. Adding $login_result = ftp_login($conn_id, $ftp_user, $ftp_pass) after $conn_id fixed my issue. Now I'm able to evaluate if it's true or false and run different code depending on the result.

How do I: PHP ftp_echo

Variation of my question on Ubuntu.SE:
This is (basically) what I'm doing when I log into a FTP:
ftp user:password#server
ftp: user:password#server: Unknown host
ftp> echo HELLO WORLD!
ftp> quit
Is it possible to "echo" over ftp in PHP?
<?php
$ftp_server = "server";
$ftp_user_name = "user";
$ftp_user_pass = "SuperSecretPassword";
$message = "Hello World!";
// connect
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// Echo Message
$upload = ftp_echo($conn_id, $message);
// close the FTP stream
ftp_close($conn_id);
?>
Maybe I'm an idiot, but all the commands I see are for pushing, pulling or doing stuff locally. Does something else act as 'ftp> echo "Hello World!"' and am I'm looking right at it without realizing it?
I think you want ftp_raw. You'd use this to put an arbitrary command to your ftp server.
<?php
$fp = ftp_connect("ftp.example.com");
/* This is the same as:
ftp_login($fp, "joeblow", "secret"); */
ftp_raw($fp, "USER joeblow");
ftp_raw($fp, "PASS secret");
?>

Categories