Php 列出目录中的文件并删除扩展名
Php list files in a directory and remove extention
我使用此 php 代码来检索存储在目录中的文件。
if ($handle = opendir('FolderPath')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry\n <br />" ;
}
}
closedir($handle);
}
此目录仅包含 PHP
个文件,我如何才能从回显结果中删除扩展名?示例:( index.php
将变为 index
)
这应该适合你:
echo basename($entry, ".php") . "\n <br />" ;
一个快速的方法是
<?php
if ($handle = opendir('FolderPath')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$info = pathinfo($file);
$file_name = basename($file,'.'.$info['extension']);
echo $file_name;
}
}
closedir($handle);
?>
$files = glob('path/to/files/*.*');
foreach($files as $file) {
if (! is_dir($file)) {
$file = pathinfo($file);
echo "<br/>".$file['filename'];
}
}
$entry = substr($entry, 0, strlen($entry) - 4);
请注意,这是一个简单快速的解决方案,如果您 100% 确定您的扩展名是 *.xxx 格式,那么它就可以完美运行。但是,如果您需要针对可能的不同扩展长度的更灵活、更安全的解决方案,则不推荐使用此解决方案。
最简单的方法是使用 the glob
function:
foreach (glob('path/to/files/*.php') as $fileName) {
//extension .php is guaranteed here
echo substr($fileName, 0, -4), PHP_EOL;
}
此处 glob
的优点是您可以消除那些讨厌的 readdir
和 opendir
调用。唯一轻微的 "disatvantage" 是 $fileName
的值也将包含路径。然而,这是一个简单的修复(只需添加一行):
foreach (glob('path/to/files/*.php') as $fullName) {
$fileName = explode('/', $fullName);
echo substr(
end($fileName),//the last value in the array is the file name
0, -4),
PHP_EOL;
}
优雅的解决方案是使用 DirectoryIterator::getBasename()
方法的 $suffix
属性。如果提供,$suffix
将在每次调用时被删除。对于已知扩展,您可以使用:
foreach (new DirectoryIterator('/full/dir/path') as $file) {
if ($file->isFile()) {
print $file->getBasename('.php') . "\n";
}
}
或者这个,作为通用解决方案:
foreach (new DirectoryIterator('/full/dir/path') as $file) {
if ($file->isFile()) {
print $file->getBasename($file->getExtension() ? '.' . $file->getExtension() : null) . "\n";
}
}
PHP 文档:http://php.net/manual/en/directoryiterator.getbasename.php
我使用此 php 代码来检索存储在目录中的文件。
if ($handle = opendir('FolderPath')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry\n <br />" ;
}
}
closedir($handle);
}
此目录仅包含 PHP
个文件,我如何才能从回显结果中删除扩展名?示例:( index.php
将变为 index
)
这应该适合你:
echo basename($entry, ".php") . "\n <br />" ;
一个快速的方法是
<?php
if ($handle = opendir('FolderPath')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$info = pathinfo($file);
$file_name = basename($file,'.'.$info['extension']);
echo $file_name;
}
}
closedir($handle);
?>
$files = glob('path/to/files/*.*');
foreach($files as $file) {
if (! is_dir($file)) {
$file = pathinfo($file);
echo "<br/>".$file['filename'];
}
}
$entry = substr($entry, 0, strlen($entry) - 4);
请注意,这是一个简单快速的解决方案,如果您 100% 确定您的扩展名是 *.xxx 格式,那么它就可以完美运行。但是,如果您需要针对可能的不同扩展长度的更灵活、更安全的解决方案,则不推荐使用此解决方案。
最简单的方法是使用 the glob
function:
foreach (glob('path/to/files/*.php') as $fileName) {
//extension .php is guaranteed here
echo substr($fileName, 0, -4), PHP_EOL;
}
此处 glob
的优点是您可以消除那些讨厌的 readdir
和 opendir
调用。唯一轻微的 "disatvantage" 是 $fileName
的值也将包含路径。然而,这是一个简单的修复(只需添加一行):
foreach (glob('path/to/files/*.php') as $fullName) {
$fileName = explode('/', $fullName);
echo substr(
end($fileName),//the last value in the array is the file name
0, -4),
PHP_EOL;
}
优雅的解决方案是使用 DirectoryIterator::getBasename()
方法的 $suffix
属性。如果提供,$suffix
将在每次调用时被删除。对于已知扩展,您可以使用:
foreach (new DirectoryIterator('/full/dir/path') as $file) {
if ($file->isFile()) {
print $file->getBasename('.php') . "\n";
}
}
或者这个,作为通用解决方案:
foreach (new DirectoryIterator('/full/dir/path') as $file) {
if ($file->isFile()) {
print $file->getBasename($file->getExtension() ? '.' . $file->getExtension() : null) . "\n";
}
}
PHP 文档:http://php.net/manual/en/directoryiterator.getbasename.php