如何获取文件名?

发布于 2024-10-03 01:00:25 字数 204 浏览 0 评论 0 原文

比如,我们有文件夹 /images/,里面有一些文件。

脚本 /scripts/listing.php

我们如何获取 listing.php 文件夹 /images/ 内所有文件的名称代码>?

谢谢。

Like, we have folder /images/, it has some files inside.

And the script /scripts/listing.php

How can we get names of the all files inside folder /images/, in listing.php?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夕嗳→ 2024-10-10 01:00:25
<?php

if ($handle = opendir('/path/to/files')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
        echo "$file\n";
    }

    /* This is the WRONG way to loop over the directory. */
    while ($file = readdir($handle)) {
        echo "$file\n";
    }

    closedir($handle);
}
?>

请参阅: readdir()

<?php

if ($handle = opendir('/path/to/files')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
        echo "$file\n";
    }

    /* This is the WRONG way to loop over the directory. */
    while ($file = readdir($handle)) {
        echo "$file\n";
    }

    closedir($handle);
}
?>

See: readdir()

丘比特射中我 2024-10-10 01:00:25

使用 glob 甚至比 readdir() 更容易:有关

$files = glob('/path/to/files/*');

glob 的更多信息

Even easier than readdir(), use glob:

$files = glob('/path/to/files/*');

more info on glob

固执像三岁 2024-10-10 01:00:25

使用 scandirdir 使这个问题变得微不足道。要获取目录中除特殊文件 ... 之外的所有文件(索引从 0 开始的数组),可以组合 scandirarray_diffarray_merge

$files = array_merge(array_diff(scandir($dir), Array('.','..')));
// $files now contains the filenames of every file in the directory $dir

Using either scandir or dir makes this problem trivial. To get all files in a directory except the special files . and .. in an array with indices starting from 0, one can combine scandir with array_diff and array_merge:

$files = array_merge(array_diff(scandir($dir), Array('.','..')));
// $files now contains the filenames of every file in the directory $dir
岁月无声 2024-10-10 01:00:25

这是使用 SPL DirectoryIterator 类的方法:

<?php

foreach (new DirectoryIterator('../images') as $fileInfo) 
{
    if($fileInfo->isDot()) continue;
    echo $fileInfo->getFilename() . "<br>\n";
}

?>

Here is a method using the SPL DirectoryIterator class :

<?php

foreach (new DirectoryIterator('../images') as $fileInfo) 
{
    if($fileInfo->isDot()) continue;
    echo $fileInfo->getFilename() . "<br>\n";
}

?>
过期以后 2024-10-10 01:00:25

只是扩展恩里科的帖子,您还需要做一些检查/修改。

class Directory
{
    private $path;
    public function __construct($path)
    {
        $path = $path;
    }

    public function getFiles($recursive = false,$subpath = false)
    {
        $files = array();
        $path = $subpath ? $subpath : $this->path;

        if(false != ($handle = opendir($path))
        {
            while (false !== ($file = readdir($handle)))
            {
                if($recursive && is_dir($file) && $file != '.' && $file != '..')
                {
                    array_merge($files,$this->getFiles(true,$file));
                }else
                {
                    $files[] = $path . $file;
                }
            }
        }
        return $files;
    }
}

用法如下:

<?php
$directory = new Directory("/");
$Files = $directory->getFiles(true);
?>

这将为您提供一个如下所示的列表:

/index.php
/includes/functions.php
/includes/.htaccess
//...

希望这会有所帮助。

just extending on Enrico's post, theres also some checks/modifications you need to do.

class Directory
{
    private $path;
    public function __construct($path)
    {
        $path = $path;
    }

    public function getFiles($recursive = false,$subpath = false)
    {
        $files = array();
        $path = $subpath ? $subpath : $this->path;

        if(false != ($handle = opendir($path))
        {
            while (false !== ($file = readdir($handle)))
            {
                if($recursive && is_dir($file) && $file != '.' && $file != '..')
                {
                    array_merge($files,$this->getFiles(true,$file));
                }else
                {
                    $files[] = $path . $file;
                }
            }
        }
        return $files;
    }
}

And the usage like so:

<?php
$directory = new Directory("/");
$Files = $directory->getFiles(true);
?>

This will get you a list like so:

/index.php
/includes/functions.php
/includes/.htaccess
//...

hoep this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文