php - ftp_rawlist/ftp_nlist causes error500 randomly

I would like to crawl my FTP server using ftp_rawlist from my web server.
It do work, but not always.
My FTP server is hosted on a windows server 2016 using IIS. It is a simple FTP account (Not FTPS and w/o certificate).
My PHP server runs PHP V7.2.30 hosted on 1and1. the memory limit is set to 128M.
the code is as follow (I am using AJAX to query the page (POST method):
display.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);
function.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;
}
All looks to work (but the year, I didn't succeed to get it yet but that is another problem).
So when I run it, ok I have the list of files and folder. I refresh ok still working, I refresh again, then its like loading and then the PHP server send an error:
Error 500 - Internal server error
Errors that happen 2 for 5 times are really hard for me to debug.
I tried to look in the log file, and I have found the requests with the error 500 code but not more info:
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" "-"
If someone have any idea, it would be appreciated.
Thanks
Answer
Solution:
So I am happy to tell you that I find how to solve the problem. But to be honest I don't understand the nature of the problem...
The solution comes from that thread : PHP FTP ftp_nlist not working, returning boolean false
just added after ftp_login() :
ftp_set_option($ftp_conn, FTP_USEPASVADDRESS, false); // set ftp option
ftp_pasv($ftp_conn, true); //make connection to passive mode
Thanks for your time @Martin
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: ftp_put(): can't open that file: no such file or directory
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.