在php中从输出的文件名中删除文件扩展名
我使用php将文件目录输出到下拉列表中 文件名和扩展名都在列表中输出,但我只希望输出文件名 我在选择文件之前使用了“basename”,但它仍然输出文件扩展名在php中从输出的文件名中删除文件扩展名,php,directory,echo,Php,Directory,Echo,我使用php将文件目录输出到下拉列表中 文件名和扩展名都在列表中输出,但我只希望输出文件名 我在选择文件之前使用了“basename”,但它仍然输出文件扩展名 <?php foreach(glob(dirname(__FILE__) . '/policies/one/*') as $filename){ $filename = basename($filename); $filename2 = substr($filename,0,strrpos($filename,"
<?php
foreach(glob(dirname(__FILE__) . '/policies/one/*') as $filename){
$filename = basename($filename);
$filename2 = substr($filename,0,strrpos($filename,".")); //To store the filename upto the last dot(.)
echo "<option value='policies/one/" . $filename . "'>".$filename2."</option>";
}
?>
这是到目前为止我的代码
basename.php
<?php
foreach(glob(dirname(__FILE__) . '/policies/one/*') as $filename){
$filename = basename($filename);
echo "<option value='policies/one/" . $filename . "'>".$filename."</option>";
}
?>
您可以使用
这可用于任何类型的文件扩展名
<?php
foreach(glob(dirname(__FILE__) . '/policies/one/*') as $filename){
$filename = basename($filename);
$filename2 = substr($filename,0,strrpos($filename,".")); //To store the filename upto the last dot(.)
echo "<option value='policies/one/" . $filename . "'>".$filename2."</option>";
}
?>
您必须知道扩展名才能提前删除它
PHP手册中的示例
$path = "/home/httpd/html/index.php";
$file = basename($path); // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
这确实删除了扩展名,但当我点击文件打开它时,它没有,因为它不再有扩展名。这是正常的:你去掉了扩展名,所以文件路径将不完整。是的,我理解为什么会发生这种情况,但是,用无法访问的文件名填充列表有什么意义呢?我编辑了我的答案,以便不打印文件的扩展名,但扩展名保留在选项中。这将解决您的第二个问题。这会删除扩展名,但当我单击文件打开它时,它不会,因为如果您删除扩展名,它将不再具有扩展名。它将如何打开@oggle0901?显然我理解,但是用无法访问的文件名填充列表有什么意义呢?这确实删除了扩展名,但是当我单击文件打开它时,它没有,因为它不再有扩展名。我已经做了相应的更改
$path = "/home/httpd/html/index.php";
$file = basename($path); // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"