简体   繁体   English

使用 FTP 和 PHP 连接到 NAS

[英]Connect to NAS with FTP and PHP

I am trying to develop a web bassed FTP client for my NAS.我正在尝试为我的 NAS 开发基于 FTP 的 web 客户端。 When I try to get a list of all the files on my NAS the script will just load forever.当我尝试获取 NAS 上所有文件的列表时,脚本将永远加载。 If I try to connect to my webhost through PHP instead, everything is fine and I can get a list of all the files.如果我尝试通过 PHP 连接到我的虚拟主机,一切都很好,我可以获得所有文件的列表。 If I run the script through the Terminal in Mac OS there is no problem getting a list of the files on my NAS.如果我通过 Mac OS 中的终端运行脚本,那么在我的 NAS 上获取文件列表是没有问题的。

Can anyone tell me why I cannot retrieve a list of the files on my NAS?谁能告诉我为什么我无法检索 NAS 上的文件列表? 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?我连接到我的 IP 地址(当然不是本地 IP:-) 并且我的端口不是默认的 21 而是 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.也许你的 nas 有防火墙。 Try enabling passive mode by using:尝试使用以下方法启用被动模式:

ftp_pasv($conn, true);

after the之后

ftp_login(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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