how can I point PHP glob to a specific directory?

老子叫甜甜 提交于 2019-12-08 14:35:20

问题


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.

<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?


回答1:


This should work:

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

Otherwise:

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



回答2:


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'));



回答3:


Just add the path to your function call :

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



回答4:


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



回答5:


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

<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_images is C:\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 ...

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



来源:https://stackoverflow.com/questions/5252243/how-can-i-point-php-glob-to-a-specific-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!