Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP FTP ftp_nlist not working, returning boolean false

Tags:

php

ftp

I have a little problem with this pice of code. The script connects, but it wont give me the folders that are located in root ... i am missing something?

    $ftp_server = "ftp.something.com";
    $ftp_user = "user";
    $ftp_pass = "pass";

    // set up a connection or die
    $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 

    // try to login
    if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) 
    {
        echo "Connected as $ftp_user@$ftp_server\n";
    } 
    else 
    {
        echo "Couldn't connect as $ftp_user@$ftp_server\n";
    }

    $contents = ftp_nlist($conn_id, ".");
    var_dump($contents);

    ftp_close($conn_id);  

    die;

It outputs

Connected as $ftp_user@$ftp_server;

and

boolean false

Why it won't list the files?

i could solve this very fast with

  file_exists("ftp//user:[email protected]") 

... but the easy part is not what im looking for, i would not learn anything

like image 783
Master345 Avatar asked Nov 21 '11 22:11

Master345


3 Answers

ftp_nlist() returns false when an error occurs. I'm guessing you need to use passive transfer:

// after ftp_login(...)
ftp_pasv($conn_id, true);

Generell, I'd recommend troubleshooting this by using a a CLI tool like ftp or a GUI-client like Filezilla. The log/output is very, very helpful.

HTH

like image 123
Till Avatar answered Nov 10 '22 05:11

Till


Don't be panic. Its easy to solve. After ftp_login() just use the code given in the below.

ftp_set_option($ftp_conn, FTP_USEPASVADDRESS, false); // set ftp option
ftp_pasv($ftp_conn, true); //make connection to passive mode

This code solved my problem.

like image 26
Omar Avatar answered Nov 10 '22 05:11

Omar


I would also just suggest confirming who and where you are to make sure that permissions and the actual results you expect are true (a little sanity check once in a while is healthy).

    echo shell_exec('whoami')." is who i am </br>";

and after you connect as user then

    echo "Current directory is now: " . ftp_pwd($conn_id) . "</br>";

if you can do these things from command line as this user and list contents of the directory then you should be well on your way.

like image 2
insitderp Avatar answered Nov 10 '22 06:11

insitderp