This article describes the method of php simulation server to achieve autoindex effect. Share it with everyone for your reference. The specific implementation method is as follows:
1.PHP code is as follows:
Copy code The code is as follows:
//File browsing program
error_reporting(0);
$pwd = empty($_GET['dir']) ? './' : $_GET['dir'];
$pwd = realpath($pwd);
if(is_file($pwd)) {
Highlight_file ($pwd);
exit;
}else
$it = new FilesystemIterator($pwd);
?>
<a href="?dir=<?php echo dirname($pwd)?>">../</a><br> <?php <br /> foreach ($it as $file){ <br /> If($file->isDir()) {<br> $fileSize = '_';<br> $fileName = $file->getFilename() . '/';<br> } elseif($file->isFile()) {<br> $fileSize = $file->getSize();<br> $fileName = $file->getFilename();<br> }<br> $date = date('Y-m-d H:i',$file->getCTime());<br> ?><a href="?dir=<?php echo $file->getRealPath()?>"><?php echo $fileName ?></a><?php echo str_pad($date, 60-strlen($fileName),' ',STR_PAD_LEFT)?><?php echo str_pad($fileSize,30,' ',STR_PAD_LEFT)?><br> <?php }?>
2. The operation effect is shown in the figure below:
I hope this article will be helpful to everyone’s PHP programming design.