PHP:没有文件扩展名的文件名 - 最好的方法?

jml*_*jml 6 php

我试图从没有扩展名的目录中提取文件名.

我正在通过以下方式解决问题:

foreach ($allowed_files as $filename) { 
  $link_filename = substr(basename($filename), 4, strrpos(basename($filename), '.'));
  $src_filename = substr($link_filename, 0, strrpos($link_filename) - 4);
  echo $src_filename;
}
Run Code Online (Sandbox Code Playgroud)

...但是如果扩展字符串长度超过3,则无法工作.我在PHP文档中查看无效.

cle*_*tus 12

PHP有一个方便的pathinfo()功能,在这里为你做腿部工作:

foreach ($allowed_files as $filename) {
  echo pathinfo($filename, PATHINFO_FILENAME);
}
Run Code Online (Sandbox Code Playgroud)

例:

$files = array(
  'somefile.txt',
  'anotherfile.pdf',
  '/with/path/hello.properties',
);

foreach ($files as $file) {
  $name = pathinfo($file, PATHINFO_FILENAME);
  echo "$file => $name\n";
}
Run Code Online (Sandbox Code Playgroud)

输出:

somefile.txt => somefile
anotherfile.pdf => anotherfile
/with/path/hello.properties => hello
Run Code Online (Sandbox Code Playgroud)