Jump to content

Recommended Posts

Here is my code for directory listing...

[code]<?

//define the path as relative
$path = "211612151419/";

//using the opendir function
$dir_handle = @opendir($path) or die("Unable to view Flash Portal in $path");

echo "Flash Portal $path<br/>";

//running the while loop
while ($file = readdir($dir_handle))
{
  if($file!="." && $file!="..")
      echo "<a href='$file'>$file</a><br/>";
}


//closing the directory
closedir($dir_handle);

?> [/code]

How can I do the following...

-ONLY list .php files
-Do NOT display the .php extension, only the name before the .php

Thanks, I'd really appreciate any help!
Link to comment
https://forums.phpfreaks.com/topic/34172-directory-list-code-need-help/
Share on other sites

I think this is one correct way of doing it.
[code]<?php

//define the path as relative
$path = "211612151419/";

//using the opendir function
$dir_handle = @opendir($path) or die("Unable to view Flash Portal in $path");

echo "Flash Portal $path<br/>";
$count = 0;
//running the while loop
while ($file = readdir($dir_handle))
{
if($count == 20) { break;} // If 20 results, stop loop
  if($file!="." && $file!="..")
    $ext = substr($file, strrpos($file, '.') + 1); // get extension
    if($ext == "php") { // if .php
    echo "<a href='$file'>$file</a><br/>"; // echo link
    }
$count++;
}


//closing the directory
closedir($dir_handle);

?> [/code]

I could be completely wrong, and if I am, Sorry! :(
try
[code]
<?php
$dir = 'c:/inetpub/wwwroot/test/';
$H = opendir($dir);
while (($file = readdir($H))!==false) {
    if ($file != '.' && $file != '..') {
        $a = explode('.', $file);
        if ($a[1]=='php') {
            $d = filemtime("$dir$file");
            $res[$a[0]] = $d;
        }
    }
}
closedir($H);

// sort by date desc
arsort($res);

// list 20 only
$res = array_slice($res, 0, 20);
foreach ($res as $f => $d) echo "$f<br>";

?>
[/code]
Sorry, I have a new code...

[code]<?php
$dir = 'c:/inetpub/wwwroot/test/';
$H = opendir($dir);
while (($file = readdir($H))!==false) {
    if ($file != '.' && $file != '..') {
        $a = explode('.', $file);
        if ($a[1]=='php') {
            $d = filemtime("$dir$file");
            $res[$a[0]] = $d;
        }
    }
}
closedir($H);

// sort by date desc
arsort($res);

// list 20 only
$res = array_slice($res, 0, 20);
foreach ($res as $f => $d) echo "$f<br>";

?>
[/code]

Theres only one problem now.
They wont show up as links!
How can I make the displayed directory files show as links?
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.