如何将 PHP glob 指向特定目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5252243/
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 20:45:08  来源:igfitidea点击:

how can I point PHP glob to a specific directory?

php

提问by Jake

So I got this code to list all jpg images inside a directory but it only works on my root directory and I don't know how to point it to my images directory.

所以我得到了这段代码来列出目录中的所有 jpg 图像,但它只适用于我的根目录,我不知道如何将它指向我的图像目录。

<ul>
<?php foreach (glob("N*T.jpg") as $image): ?>
    <li>
        <a href="<?php echo str_replace("T", "F", $image); ?>">
            <img src="<?php  echo "$image"; ?>">
        </a>
    </li>
<?php endforeach; ?>
</ul>

Can anyone help me with that?

任何人都可以帮助我吗?

回答by seriousdev

This should work:

这应该有效:

glob('images/N*T.jpg');

Otherwise:

除此以外:

chdir('images');
glob('N*T.jpg');

回答by Gordon

Just prepend the path to the function call.

只需添加函数调用的路径即可。

glob('/path/to/directory/N*T.jpg');

Note that the resulting array will contain the prepended path as well. If you don't want that do

请注意,结果数组也将包含前置路径。如果你不想那样做

array_map('basename', glob('/path/to/directory/N*T.jpg'));

回答by krtek

Just add the path to your function call :

只需将路径添加到您的函数调用:

glob("/my/path/to/directory/N*T.jpg")

回答by u476945

$files = glob("/path/to/directory/N*T.jpg");

回答by Andris

As understand globstarts with root directory. For example i see image with this

据了解,glob从根目录开始。例如我看到这个图像

<img src="<?php 
echo $_SERVER['REQUEST_SCHEME']. '://'. $_SERVER['SERVER_NAME']. '/public_images/logo_image_.gif';
?>"></img>

Then try

然后试试

echo pathinfo( glob( 'public_images/logo_image_.*' )[0] , PATHINFO_EXTENSION). '  <br/>';

and see gif. Location of public_imagesis C:\wamp\www\public_images

并看到gif。的位置public_imagesC:\wamp\www\public_images

But for example with

但例如

echo pathinfo( glob( $_SERVER['REQUEST_SCHEME']. '://'. $_SERVER['SERVER_NAME']. 'public_images/logo_image_.*' )[0] , PATHINFO_EXTENSION). '  <br/>';

see Notice: Undefined offset: 0 in C:\wamp\www\show_content.php on line ...

Notice: Undefined offset: 0 in C:\wamp\www\show_content.php on line ...

However if from file located at C:\wamp\www\some_dircall file located at C:\wamp\www\public_images, then need to use echo pathinfo( glob( '../public_images/logo_image_.*' )[0] , PATHINFO_EXTENSION)

但是,如果来自文件位于C:\wamp\www\some_dir调用文件位于C:\wamp\www\public_images,则需要使用echo pathinfo( glob( '../public_images/logo_image_.*' )[0] , PATHINFO_EXTENSION)