php 通过 FTP 获取文件列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4088805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:54:46  来源:igfitidea点击:

Getting the list of files over FTP

phpftp

提问by sikas

I want to print the list of files and only files from an FTP server, here is what I could accomplish.

我想打印文件列表,并且只打印来自 FTP 服务器的文件,这是我可以完成的。

<?php
    $ftp_server = "my ftp server";
    $conn_id = ftp_connect($ftp_server);
    $ftp_user_name = "ftp username";
    $ftp_user_pass = "ftp password";
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    $contents = ftp_nlist($conn_id, '/');
    for ($i = 0 ; $i < count($contents) ; $i++)
        echo "<li>" . substr($contents[$i],1) . "</li>";
    ftp_close($conn_id);
?>

but this prints the names of files and folders. How can I just print the names of files (files may not have extensions!)

但这会打印文件和文件夹的名称。我怎么能只打印文件名(文件可能没有扩展名!)

采纳答案by Jacob Mattison

Options:

选项:

1) you can use ftp_rawlistinstead of ftp_nlistto get the full listing for the file/directory, which should indicate whether it's a directory. However, the format of that listing will depend on the operating system of the ftp server. For example, on a unix/linux system the raw listing might look something like this:

1)您可以使用ftp_rawlist而不是ftp_nlist获取文件/目录的完整列表,它应该表明它是否是一个目录。但是,该列表的格式将取决于 ftp 服务器的操作系统。例如,在 unix/linux 系统上,原始列表可能如下所示:

drwxrwxr-x  3 jm72 jm72  4096 Nov  2 16:39 myDir
-rw-rw-r--  1 jm72 jm72   257 Nov  2 16:39 myFile

where the "d" in the first column will tell you it's a directory. Not sure what it would look like on a Windows server.

第一列中的“d”将告诉您它是一个目录。不确定它在 Windows 服务器上会是什么样子。

2) for each file name you return, try to CD into it. If you can, it's a directory!

2) 为您返回的每个文件名,尝试将 CD 放入其中。如果可以,它是一个目录!

if (ftp_chdir($conn_id, substr($contents[$i],1)) {
  //it's a directory, don't include it in your list
  ftp_cdup($conn_id) //don't forget to go back up to the directory you started in!
}

回答by Justin Ethier

Here is a script that will do it for you, courtesy of a poster on ftp_nlist (PHP Docs):

这是一个可以为您完成的脚本,由ftp_nlist (PHP Docs)上的海报提供:

<?php

//identify directories

function ftp_is_dir($dir) {
  global $ftp_connect;
  if (@ftp_chdir($ftp_connect, $dir)) {
       ftp_chdir($ftp_connect, '..');
       return true;
  } else {
       return false;
  }
}
$ftp_nlist = ftp_nlist($ftp_connect, ".");

//alphabetical sorting

sort($ftp_nlist);
foreach ($ftp_nlist as $v) {

//1. ftp_is_dir() is true => directory
  if (ftp_is_dir($v)) {

//output as [ directory ]
      echo "[ " . $v . " ]<br />\n";
  }
}
foreach ($ftp_nlist as $v) {

//2. ftp_is_dir() is false => file
  if (!ftp_is_dir($v)) {

//output as file
      echo "" . $v . "<br />\n";
  }
}
?>

回答by RobertPitt

if you use the ftp_rawlist like so:

如果您像这样使用 ftp_rawlist:

$rawfiles = ftp_rawlist($conn, true); //true being for recursive

foreach ($rawfiles as $rawfile)
{
    $info = preg_split("/[\s]+/", $rawfile, 9);
    if($info[0]{0} == 'd')
    {
        //Directory
    }else
    {
        //File
        $size = byteconvert($info[4]);
        $chmod = chmodnum($info[0]);
        $date = strtotime($info[6] . ' ' . $info[5] . ' ' . $info[7]);
    }
}

Should get you closer to your goal.

应该让你更接近你的目标。

回答by Lee Blake

You could also use the ftp_mdtm()function. This always returns an error (-1) when it fails which would indicate a directory since this function doesn't work for directories. The one caveat is that it doesn't work on some systems (the manual doesn't state which ones), so you'd need to test if it worked first.

您也可以使用该ftp_mdtm()功能。这在失败时总是返回错误 (-1),这将指示一个目录,因为此功能不适用于目录。一个警告是它在某些系统上不起作用(手册没有说明哪些系统),所以你需要先测试它是否有效。

回答by aesede

If you are in the same server, you can do:

如果您在同一台服务器中,则可以执行以下操作:

$contents = ftp_nlist($conn_id, '/');
// set the path to the folder
$path = '/home/user/public_html/my_folder/';
foreach ( $contents as $item ) {
    if ( is_file( $path . $item ) ) {
        echo $item . PHP_EOL;
    } else {
        continue;
    }
}

For remote server you can check if file exists with other methods.

对于远程服务器,您可以使用其他方法检查文件是否存在。