I'm trying to build a script that will connect to my servers and download files for backup. I'm using the following code:

//*****FTP BACKUP PORTION*****\\

$curr_date = date("Y-m-d");
echo "<br>".$curr_date."<br>";

$backup_dir = "/backup";
mkdir($backup_dir."/temp/");
mkdir($backup_dir."/".$curr_date);




$ftp_mode = FTP_BINARY;  //either FTP_BINARY or FTP_ASCII
$directory_list = Array();


// set up basic connection
$ftp_connection = ftp_connect($ftp_host);

// login with username and password
$login_result = ftp_login($ftp_connection, $ftp_username, $ftp_password);

// check connection
if ((!$ftp_connection) || (!$login_result)){
    echo "FTP connection has failed!<BR>\n";
    echo "Attempted to connect to ".$ftp_host." for user ".$ftp_username.".<BR>\n";
}
else {
    echo "Connected to $ftp_host, for user $ftp_username";
}

echo "PWD: " . ftp_pwd($ftp_connection) . "<BR>\n";

    //try to change the directory to defalut $ftp_start_dir
if (ftp_chdir($ftp_connection, $ftp_start_dir)) {

    $curr_dir = ftp_pwd($ftp_connection);
    echo "Current directory is now: ".$curr_dir."<BR>\n";
    array_push($directory_list, $curr_dir);

}
else {
    echo "Couldn't change directory to ".$ftp_start_dir." on remote server.<BR>\n";
}

for($j = 0; ISSET($directory_list[$j]); $j++){

    if (ftp_chdir($ftp_connection, $directory_list[$j])) {

        $curr_dir = ftp_pwd($ftp_connection);
        echo "Current directory is now: ".$curr_dir."<BR>\n";
    }
    else {
        echo "Could not change directory to: ".$directory_list[$j];
    }

    $curr_file_list = ftp_nlist($ftp_connection, ".");
    $curr_dir = $directory_list[$j];



echo "<br><br><Br>";
var_dump($curr_file_list);


I get the following output:


Running backup for site boscoandcourtneydotcom

2007-06-03
Connected to ftp.boscoandcourtney.com, for user boscoandcourtneyPWD: /
Current directory is now: /httpdocs/img/icons
Current directory is now: /httpdocs/img/icons


bool(false)

Why would var_dump() produce bool(false) when I know there are files in that directory? Thanks for any help!

    it shouldn't. are you positive that there are indeed files in there that you can see?

      sid wrote:

      it shouldn't. are you positive that there are indeed files in there that you can see?

      This is very strange. I was running this code from a machine that I just installed Fedora7 on. I moved the file to an XP machine, and got this output:

      Running backup for site boscoandcourtneydotcom
      
      2007-06-03
      Connected to ftp.boscoandcourtney.com, for user boscoandcourtneyPWD: /
      Current directory is now: /httpdocs/img/icons
      Current directory is now: /httpdocs/img/icons
      
      
      
      array(3) { [0]=> string(11) "success.gif" [1]=> string(4) "tabs" [2]=> string(8) "fail.gif" }
      
      
      
      

      Which tells me there is a problem on the linux machine. Any idea of what could be causing this?

        Is display_errors turned On and error_reporting set to E_ALL ?

        EDIT: Here's a user contributed note (link) on the manual page for [man]ftp_nlist/man that might help:

        Sometimes, you won't be able to get folder content because of NAT or Firewall issues on your server. As a result you need to put ftp connection into a passive mode:

        <?php
        
        // set up basic connection
        $conn_id = ftp_connect($ftp_server);
        
        // login with username and password
        $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
        
        // enabling passive mode
        ftp_pasv( $conn_id, true );
        
        // get contents of the current directory
        $contents = ftp_nlist($conn_id, ".");
        
        // output $contents
        var_dump($contents);
        
        ?> 
          bradgrafelman wrote:

          Is display_errors turned On and error_reporting set to E_ALL ?

          EDIT: Here's a user contributed note (link) on the manual page for [man]ftp_nlist/man that might help:

          Thanks a lot, passive mode seemed to take care of the problem. Thanks again!

            Write a Reply...