开发者

how can I point PHP glob to a specific directory?

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?


This should work:

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

Otherwise:

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


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


Just add the path to your function call :

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


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


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜