我有问题连接到我的ftp服务器在PHP。我能够从cmd提示符中使用telnet命令登录,因此服务器/用户/密码是正确的,但是当我尝试使用PHP连接时,我得到一个错误:
警告:ftp_login() [function。ftp-login]:登录错误。在/爱马仕/bosweb/web232/b2323/ipg…
当我注释掉我的server/user/pass并替换为一个公共匿名ftp服务器时,代码工作了(如下所示)。过去一周我一直在寻找答案,但仍然不知道我错过了什么,也不知道该去哪里找。另外,我是一个php/服务器新手,所以它可能是一些非常简单的东西。任何帮助都非常感谢!
<?php
/*
// (does not work)
$ftp_server = "ftp.*****.com";
$ftp_user = "******";
$ftp_password = "*****";
*/
$ftp_server = "ftp.gnu.org";
$ftp_user = "anonymous";
$ftp_password = "none";
/* connect */
$ftp_connection = @ftp_connect($ftp_server);
if (!$ftp_connection) die('could not connect.');
/* login */
$ftp_login = @ftp_login($ftp_connection, $ftp_user, $ftp_password);
if (!$ftp_login) die('could not login.');
/* enter passive mode */
$ftp_passive = @ftp_pasv($ftp_connection, true);
if (!$ftp_passive) die('could not enable passive mode.');
/* get listing */
$ftp_listing = ftp_nlist($ftp_connection, ".");
foreach ($ftp_listing as $file){
echo "<div>".$file."</div>";
}
ftp_close($ftp_connection);
?>
您可能会发现完全放弃ftp扩展名而只使用正常的文件系统功能更容易:
// Download a file and store the data in $data
$data = file_get_contents('ftp://username:password@ftp.domain.tld/somefile.txt');
// Loop the contents of the root directory
$dp = opendir('ftp://username:password@ftp.domain.tld/');
while ($file = readdir($dp)) {
// Do stuff
}
closedir($dp);
// Upload a file
file_put_contents('ftp://username:password@ftp.domain.tld/somefile.txt','This is the file data');
// Upload a file from the local file system
$local = fopen('/path/to/my/file.ext','r');
$remote = fopen('ftp://username:password@ftp.domain.tld/somefile.txt','w');
stream_copy_to_stream($local,$remote);
您可能会发现这更容易使用,因为许多腿部工作已经为您完成了,并且它可能使您的代码更具可移植性-并不是每个服务器都安装了FTP扩展。您可以通过创建流上下文来做更复杂的事情,它可以与大多数文件系统函数一起使用。
抱歉,新手php错误。我试图从该服务器上的php代码访问我的ftp服务器…我想这行不通。我从另一个服务器运行代码,工作正常。谢谢!