문제

I'm working on a small script and I want to list the contents of a directory, make them into hyperlinks, and then edit those hyperlinks to look pretty (I.e. not show an ugly super long path name), then limit the number files echoed back to the browser. Also, I need the most recent files echoed back only.

I was thinking about using this:

<?php
 $path = "/full/path/to/files";
 if ($handle = opendir($path)) {
   while (false !== ($file = readdir($handle)))
          {
                  if ($file != "." && $file != "..")
          {
                          $files .= '<a href="'.$file.'">'.$file.'</a>';
                  }
           }
  closedir($handle);
  }
?>

or this:

<?php
$sub = ($_GET['dir']);
$path = 'enter/your/directory/here/';
$path = $path . "$sub";
$dh = opendir($path);
$i=1;
while (($file = readdir($dh)) !== false) {
        if($file != "." && $file != "..") {
                        if (substr($file, -4, -3) =="."){
                        echo "$i. $file <br />";
                        }else{                  
                echo "$i. <a href='?dir=$sub/$file'>$file</a><br />";
                  }
                $i++;
        }
}
closedir($dh);
?>

But I dont want to list the files like this:

C:/example/example2/Hello.pdf

I want to edit the variable. Is that possible? To make it say something as simple as "Hello."

I want to limit the amount of files listed as well. For example: only list the first 5 files, or last 5, etc. Is there a function or some kind of parameter for that?

I appreciate any help or push in the right direction. Thanks

도움이 되었습니까?

해결책

I'm on my phone so providing a code example will be tough. Why not iterate through the directories, storing the file name in an array, with the absolute path as the value for that key?

EDIT: You can use basename to aid you in doing this.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top