简体   繁体   中英

PHP: list files on ftp server?

can't get it done!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>FTP Download</title>
</head>
<body>
    <?php

    set_time_limit(300);//for setting 
    $path='/userupload';
    $ftp_server='202. …';
    $ftp_server_port="21";
    $ftp_user_name='al…';
    $ftp_user_pass="mypassword";

    // 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);

    // check connection and login result
    if ((!$conn_id) || (!$login_result)) { 
        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
        var_dump($contents);
    }

    // close the FTP connection
    ftp_close($conn_id);

    ?>
</body>
</html>

any idea what I'm doing wrong? it always returns "Success bool(false)"! So the connection is working, however the files won't get listed. Any ideas?

regards

what I'm doing wrong?

you've lack of debugging.

checking only connect result is not enough. have to check every operation as well.

did ftp_pasv() worked?
is there /userupload dir?

what Shrapnel is saying is you can have more debugging than youve got if you've got

//enabling passive mode ftp_pasv( $conn_id, true );

then you can use if(ftp_pasv( $conn_id, true )){// it worked :) }else{// it didnt work :( }

But what I think is wrong is your path, if the home folder of your ftp user lands them inside here /useruploads/ im here then to list the files your path should be $path='/'; otherwise what your telling the ftp to do is list the directory "useruploads/useruploads" which isnt going to exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM