开发者

Connect to NAS with FTP and PHP

开发者 https://www.devze.com 2023-03-20 17:07 出处:网络
I am trying to develop a web bassed FTP client for my NAS. When I try to get a list of all the files on my NAS the script will just load forever. If I try to connect to my webhost through PHP instead,

I am trying to develop a web bassed FTP client for my NAS. When I try to get a list of all the files on my NAS the script will just load forever. If I try to connect to my webhost through PHP instead, everything is fine and I can get a list of all the files. If I run the script through the Terminal in Mac OS there is no problem getting a list of the files on my NAS.

Can anyone tell me why I cannot retrieve a list of the files on my N开发者_如何学CAS? Does it matter that I connect to my IP address (of course not the local IP :-)) and my port is not the default 21 but 2121?

Below is my script.

// Set IP and port
define("FTP_CONNECT_IP", "");
define("FTP_CONNECT_PORT", "");

// Set username and password
define("FTP_LOGIN_USER", "");
define("FTP_LOGIN_PASS", "");

// Set directory to open
$dir = ($_GET['dir']) ? $_GET['dir'] : ".";

// Connect to FTP server
$conn = ftp_connect(FTP_CONNECT_IP, FTP_CONNECT_PORT); // Timeout is not set, default is 90 seconds

// Log into FTP srever
$login_result = ftp_login($conn, FTP_LOGIN_USER, FTP_LOGIN_PASS);

// Print details about directory
// Set what directory name to show
$dir_name = ($dir == ".") ? "/" : "/" . $dir;

echo "Current dir: " . $dir_name . "<br /><br />";

foreach (ftp_nlist($conn, $dir) as $element) {
    $element_name = str_replace($dir . "/", "", $element);

    // Check if element is a file or a directory
    // If size is -1 then the element is a directory
    if(ftp_size($conn, $element) == -1) {
        echo "<a href=\"index.php?dir=" . $element . "\">" . $element_name . "</a><br />";
    }
    else {
        echo "<a href=\"http://" . $element . "\" target=\"_blank\">" . $element_name . "</a><br />";
    }
}

// Close connection
ftp_close($conn);


Perhaps your nas has a firewall. Try enabling passive mode by using:

ftp_pasv($conn, true);

after the

ftp_login(...)
0

精彩评论

暂无评论...
验证码 换一张
取 消