【问题标题】:ftp_rawlist/ftp_nlist causes error500 randomlyftp_rawlist/ftp_nlist 随机导致error500
【发布时间】:2020-05-08 04:34:49
【问题描述】:

我想使用来自我的网络服务器的 ftp_rawlist 抓取我的 FTP 服务器。

它确实有效,但并非总是如此。

我的 FTP 服务器托管在使用 IIS 的 windows server 2016 上。 这是一个简单的 FTP 帐户(不是 FTPS 和无证书)。

我的 PHP 服务器运行托管在 1and1 上的 PHP V7.2.30。 内存限制设置为128M。

代码如下(我使用AJAX查询页面(POST方式):

显示.php

$ftp_server = "xxx.xxx.xxx.xxx";
$ftp_username = "xxxxxxxxxxx";
$ftp_userpass = "xxxxxxxxxxxxxx";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to the FTP server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

$scan = ftp_get_filelist($ftp_conn, '.');
var_dump($scan);

函数.php

function ftp_get_filelist($ftp_conn, $path){
 $Win=false;
 $contents = ftp_rawlist ($ftp_conn, $path);
 $Output = array();
 $i = 0;

 foreach ($contents as $Current) {
  $Split = preg_split('[ ]', $Current, 9, PREG_SPLIT_NO_EMPTY);
  if ($Split[0] != 'total') {
   $Output[$i]['isdir']     = ($Split[0] {0} === 'd');
   $Output[$i]['perms']     = $Split[0];
   $Output[$i]['number']    = $Split[1];
   $Output[$i]['owner']     = $Split[2];
   $Output[$i]['group']     = $Split[3];
   $Output[$i]['size']      = $Split[4];
   $Output[$i]['month']     = $Split[5];
   $Output[$i]['day']       = $Split[6];
   $Output[$i]['time']   = $Split[7];
   $Output[$i]['full_date'] = '2020-'.$Split[5].'-'.$Split[6].' '.$Split[7];
   $Output[$i]['name']      = $Split[8];
   $i++;
  }
 }
 return !empty($Output) ? $Output : FALSE;

}

看起来一切正常(但是这一年,我还没有成功,但这是另一个问题)。

所以当我运行它时,好的,我有文件和文件夹的列表。 我刷新确定仍在工作, 我再次刷新,然后就像加载一样,然后PHP服务器发送错误:

错误 500 - 内部服务器错误

5 次出现 2 次的错误对我来说真的很难调试。

我尝试查看日志文件,发现请求带有错误 500 代码但没有更多信息:

xxx.xxx.xxx.xxx - - [08/May/2020:06:05:50 +0200] "POST display.php HTTP/1.1" 500 669 www.xxxxxx.com "https://xxxxxx.com/display.php" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36" "-"

如果有人有任何想法,将不胜感激。

谢谢

【问题讨论】:

  • 为什么你认为它是由ftp_rawlist引起的?看来你必须调试的地方。那你怎么知道呢?
  • 我认为它来自 ftp_rawlist 因为当我用 ftp_nlist 替换它时我没有问题。但 ftp_nlist 不适合我的需要,因为它只列出文件而不是文件夹
  • 这似乎不太可能,除非问题是您的脚本达到内存限制(由于ftp_rawlist 的输出较大)或脚本执行限制。
  • 等等,让我再次确认我之前的声明。
  • 所以你是对的,我使用 ftp_nlist 做了更多的压力测试,实际上它在某些时候反应相同。被抓取的 FTP 文件夹有 1 个文本文件和一个文件夹。

标签: php ftp


【解决方案1】:

所以我很高兴地告诉你,我找到了解决问题的方法。 但老实说,我不明白问题的本质......

解决方案来自该线程: PHP FTP ftp_nlist not working, returning boolean false

刚刚在 ftp_login() 之后添加:

ftp_set_option($ftp_conn, FTP_USEPASVADDRESS, false); // set ftp option
ftp_pasv($ftp_conn, true); //make connection to passive mode

感谢您抽出宝贵时间@Martin

【讨论】:

  • 如果 FTP_USEPASVADDRESSftp_pasv 是唯一的解决方法,我不明白你的脚本是如何工作的。无论如何,至少它现在可以工作了:)
猜你喜欢
  • 1970-01-01
  • 2022-07-04
  • 2012-05-11
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-15
相关资源
最近更新 更多