douyanpeng0748 2015-04-28 19:25
浏览 24
已采纳

删除目录中获取文件列表中的点

How can I remove the dot before the last trailing slash?

Example with dot output in HTML table:

Name                                Type          Size      Last Modified
http://www.airsahara.in./fares/     text/x-php    662       2014-09-04
http://www.airsahara.in./           text/x-php    1681      2014-09-04

Here is the php code I have which allows me to only read certain directories based on what is in the ignore array. I just can't figure out how to get rid of the . before the trailing slash /.

I need to remove that dot so the url can be correct.

<?
$ignore = array( 'images', 'css', 'includes', 'cgi-bin', 'xml-sitemap.php' );

  function getFileList($dir, $recurse=false) {
  global $ignore;

    $retval = array();

    // open pointer to directory and read list of files
    $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
    while ( false !== ( $entry = $d->read() ) ) {

        // Check if this dir needs to be ignored, if so, skip it.
        if ( in_array( utf8_encode( $entry ), $ignore ) )
            continue;

      // skip hidden files
      if($entry[0] == ".") continue;
      if(is_dir("$dir$entry")) {
        $retval[] = array(
          "name" => "$dir$entry/",
          "type" => filetype("$dir$entry"),
          "size" => 0,
          "lastmod" => filemtime("$dir$entry")
        );
        if($recurse && is_readable("$dir$entry/")) {
          $retval = array_merge($retval, getFileList("$dir$entry/", true));
        }
      } elseif(is_readable("$dir$entry")) {
        $retval[] = array(
          "name" => "$dir",
          "type" => mime_content_type("$dir$entry"),
          "size" => filesize("$dir$entry"),
          "lastmod" => filemtime("$dir$entry")
        );
      }
    }
    $d->close();

    return $retval;
  }

  $dirlist = getFileList("./", true);

    // output file list in HTML TABLE format
  echo "<table border=\"1\">
";
  echo "<thead>
";
  echo "<tr><th>Name</th><th>Type</th><th>Size</th><th>Last Modified</th></tr>
";
  echo "</thead>
";
  echo "<tbody>
";
  foreach($dirlist as $file) {
      if($file['type'] != 'text/x-php') continue;
    echo "<tr>
";
    echo "<td>http://www.airsahara.in{$file['name']}</td>
";
    echo "<td>{$file['type']}</td>
";
    echo "<td>{$file['size']}</td>
";
    echo "<td>",date('Y-m-d', $file['lastmod']),"</td>
";
    echo "</tr>
";
  }
  echo "</tbody>
";
  echo "</table>

";
?>
  • 写回答

1条回答 默认 最新

  • doupingpeng7567 2015-04-28 19:30
    关注

    You could perform a string replace -

    "name" => str_replace('./','/', $dir),
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私