Jake
Jake

Reputation: 1390

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?

Upvotes: 24

Views: 38125

Answers (5)

Andris
Andris

Reputation: 1442

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)

Upvotes: 1

seriousdev
seriousdev

Reputation: 7656

This should work:

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

Otherwise:

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

Upvotes: 44

KJYe.Name
KJYe.Name

Reputation: 17169

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

Upvotes: 1

krtek
krtek

Reputation: 26597

Just add the path to your function call :

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

Upvotes: 1

Gordon
Gordon

Reputation: 316979

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

Upvotes: 15

Related Questions