简体   繁体   English

如何将FTP服务器的文件列表分隔为新行

[英]How to get FTP server's file list separated by new line

I am trying to access a file list on a FTP server. 我试图访问FTP服务器上的文件列表。 My code is working fine but all results are showing on one line, I want links displayed as links, one per line. 我的代码工作正常,但所有结果显示在一行,我希望链接显示为链接,每行一个。

This is what I tried: 这是我试过的:

$ftp_server = "abc"; 
 $ftp_user = "xyz";
 $ftp_password = "123";
 $path = "/";
if(ftp_login($conn,$ftp_user,$ftp_password))
{
 $contents= ftp_nlist($conn, $path); 
echo implode("\n",$contents)    ;

 }
?>

results are like: 结果如下:

file1 file2 file3  

I want it to be like: 我希望它像:

file1
file2
file3

If you are on Windows you may actually need \\r\\n instead of just \\n due to the way that Windows handles new lines. 如果你在Windows上,由于Windows处理新行的方式,你实际上可能需要\\r\\n而不只是\\n

PHP has a handy constant called PHP_EOL which will automatically use the correct one. PHP有一个名为PHP_EOL常量,它将自动使用正确的常量。

If you are outputting the text in HTML then rather than a new line, you actually need to separate each file with a line break. 如果要输出HTML的文本而不是新行,则实际上需要使用换行符分隔每个文件。 The HTML for a line break is <br> . 换行符的HTML<br>

Try the following to see which one works for you. 请尝试以下方法,了解哪一个适合您。

echo implode(PHP_EOL, $contents); // New line
echo implode('<br>', $contents); // HTML line break

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

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