php 如何在php中使用glob()只返回文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14366928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:08:29  来源:igfitidea点击:

How to return just file name using glob() in php

phpyii

提问by Ashish

How can I just return the file name. $image is printing absolute path name?

我怎么能只返回文件名。$image 正在打印绝对路径名?

<?php
$directory = Yii::getPathOfAlias('webroot').'/uploads/';
$images = glob($directory . "*.{jpg,JPG,jpeg,JPEG,png,PNG}", GLOB_BRACE);
 foreach($images as $image)
   echo $image
?>

All I want is the file name in the specific directory not the absolute name.

我想要的只是特定目录中的文件名而不是绝对名称。

采纳答案by ernie

Instead of basename, you could chdir before you glob, so the results do not contain the path, e.g.:

而不是basename,您可以在 glob 之前 chdir ,因此结果不包含路径,例如:

<?php
$directory = Yii::getPathOfAlias('webroot').'/uploads/';
chdir($directory); // probably add some error handling around this
$images = glob("*.{jpg,JPG,jpeg,JPEG,png,PNG}", GLOB_BRACE);
 foreach($images as $image)
   echo $image;
?>

This is probably a little faster, but won't make any significant difference unless you have tons of files

这可能会快一点,但除非您有大量文件,否则不会产生任何显着差异

回答by Asgaroth

Use php's basename

使用 php 的基本名称

Returns trailing name component of path

返回路径的尾随名称组件

<?php
$directory = Yii::getPathOfAlias('webroot').'/uploads/';
$images = glob($directory . "*.{jpg,JPG,jpeg,JPEG,png,PNG}", GLOB_BRACE);
 foreach($images as $image)
   echo basename($image);
?>

回答by Maxxer

One-liner:

单线:

$images = array_map('basename', glob($directory . "*.{jpg,JPG,jpeg,JPEG,png,PNG}", GLOB_BRACE));

回答by David Harris

Use basename()

basename()

echo basename($image);

You can also remove the extension like this:

您还可以像这样删除扩展名:

echo basename($image, '.php');

回答by Ascherer

Take a look at pathinfo

看一下pathinfo

http://php.net/manual/en/function.pathinfo.php

http://php.net/manual/en/function.pathinfo.php

Pretty helpful function

很实用的功能

回答by Alex Standiford

If you're nervous about the unintended consequences of changing the working directory, you can store the current dir with getcwd()before changing, then simply use that value to switch back after you've done everything you need to do in that dir.

如果您对更改工作目录的意外后果感到紧张,您可以getcwd()在更改之前存储当前目录,然后在您完成该目录中需要执行的所有操作后使用该值切换回来。

  <?php
  $directory = Yii::getPathOfAlias('webroot').'/uploads/';
  $working_dir = getcwd();
  chdir($directory);
  $files = glob("*.{jpg,JPG,jpeg,JPEG,png,PNG}", GLOB_BRACE);
  chdir($working_dir);
  ?>

回答by Stanislav Prusac

Example extracting only file names and converting in new array of filenames width extension.

仅提取文件名并转换为新的文件名宽度扩展数组的示例。

$dir =  get_stylesheet_directory();//some dir - example of getting full path dir in wordpress
$filesPath = array_filter(glob($dir . '/images/*.*'), 'is_file');

$files = array();       
foreach ($filesPath as $file) 
{
   array_push($files, basename($file));
}