Windows 主机上的 php 脚本不列出 ftp 文件(ftp_rawlist 问题)
我正在尝试在 Windows 共享主机上使用 php ftp 函数列出来自外部 FTP 服务器的一些文件,但遇到了几个问题。
我首先尝试使用 ajaxplorer 和 net2ftp 等几个 Web 应用程序,但我感到沮丧,我决定制作一个非常基本的脚本进行测试。
<?php
$ftp_server = "alinuxftpserver";
$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");
// change temp folder (windows)
putenv("TMP=D://inetpub//webs//domain//net2ftp//tmp");
echo getenv('TMP');
// 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";
}
if(ftp_pasv( $conn_id, true ))
echo "Passive mode, it worked<br/>";
else
echo "Passive mode, it didn't work<br/>";
$contents = ftp_rawlist($conn_id, ".");
var_dump($contents);
ftp_close($conn_id);
die;
?>
在我的本地主机(linux)上它返回一个数组,而在托管它的 Windows 上返回:
Warning: ftp_rawlist() [function.ftp-rawlist]: php_connect_nonb() failed: No such file or directory (2) in D:\inetpub\webs\domain\ftp.php on line 26
bool(false)
无法理解..目录应该是外部 ftp 服务器上的“/”,当然还有一些文件和文件。文件夹(2 个文件夹和 1 个文件).. 事实上,在我的 MAMP 安装中它运行良好。
托管人员告诉我服务器配置没问题。
I'm trying to list some files from an external FTP server using php ftp functions on a Windows shared hosting, but I'm having several problems.
I firstly tried with a couple of web applications like ajaxplorer and net2ftp, but I got frustrated and I decided to make a very basic script for testing..
<?php
$ftp_server = "alinuxftpserver";
$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");
// change temp folder (windows)
putenv("TMP=D://inetpub//webs//domain//net2ftp//tmp");
echo getenv('TMP');
// 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";
}
if(ftp_pasv( $conn_id, true ))
echo "Passive mode, it worked<br/>";
else
echo "Passive mode, it didn't work<br/>";
$contents = ftp_rawlist($conn_id, ".");
var_dump($contents);
ftp_close($conn_id);
die;
?>
On my localhost (linux) it returns an array, while on the windows hosting it returns:
Warning: ftp_rawlist() [function.ftp-rawlist]: php_connect_nonb() failed: No such file or directory (2) in D:\inetpub\webs\domain\ftp.php on line 26
bool(false)
Can't understand.. the directory should be "/" on the external ftp server and of course there are some files & folders (2 folders and 1 file).. In fact on my MAMP installation it works well.
Hosting guys told me that the server configuration is ok.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 ftp_pasv($conn_id, true);某些 ftp 连接只能在被动模式下工作
use ftp_pasv($conn_id, true); some ftp connections will work in passive mode only
我不是 100% 确定,但我想,你应该使用 $contents = ftp_rawlist($conn_id, "/");
而不是
$contents = ftp_rawlist($conn_id, ".");
I'm not 100% sure, but I guess, you should use
$contents = ftp_rawlist($conn_id, "/");
instead of$contents = ftp_rawlist($conn_id, ".");
检查您的 FTP 服务器日志。就我而言,pasv_address 设置为错误的 IP 地址。
Check your FTP server logs. In my case, the pasv_address was set to a wrong IP address.
迟到总比不到好……我也遇到了同样的问题。对于 Linux 服务器,一切都运行良好,但对于 Windows Server(许多版本),我们遇到了很多问题,包括 ftp_nlist() 返回空数组。这对我们有用,但我不知道为什么!
Better late than never... I had the same problem. With a Linux server everything worked great, but with Windows Server (many versions) we had many problems, including with ftp_nlist() returning an empty array. This worked for us, but I don't know why!