Mike
Mike

Reputation: 607

Remove dot in get list of files in directory

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\">\n";
  echo "<thead>\n";
  echo "<tr><th>Name</th><th>Type</th><th>Size</th><th>Last Modified</th></tr>\n";
  echo "</thead>\n";
  echo "<tbody>\n";
  foreach($dirlist as $file) {
      if($file['type'] != 'text/x-php') continue;
    echo "<tr>\n";
    echo "<td>http://www.airsahara.in{$file['name']}</td>\n";
    echo "<td>{$file['type']}</td>\n";
    echo "<td>{$file['size']}</td>\n";
    echo "<td>",date('Y-m-d', $file['lastmod']),"</td>\n";
    echo "</tr>\n";
  }
  echo "</tbody>\n";
  echo "</table>\n\n";
?>

Upvotes: 0

Views: 1566

Answers (1)

Jay Blanchard
Jay Blanchard

Reputation: 34426

You could perform a string replace -

"name" => str_replace('./','/', $dir),

Upvotes: 1

Related Questions