简体   繁体   中英

How do I hide the .php extension when listing files in php?

I have the following code to show the files in a folder in php.

<html>
<head>
<?php require '/opt/bitnami/apps/wordpress/htdocs/head.php';?>
</head>
<body>
<br>
<br>
<br>
<br>
<?php
  if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != ".."&& $file != "posts.php") {
        $thelist .= '<a href="'.$file.'">'.$file.'</a><hr>';
      }
    }
    closedir($handle);

  }
?>
<center>
<h1>files</h1>
<br>
<br>
<div style=background-color:white;color:black;>
<br>
<p1 style="font-size: 25px;"><?php echo $thelist;?></p1><br>

<hr>
</div>
</center>
</body>
</html>

So, I do not want the.php file extension to not show up when they are being listed. For example if there is a file called test.php, I want it to just list test. What code should I add/change to make this work?

Thanks for any help.

You can try using str_replace()

str_replace(".php","",$file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM