Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I point PHP glob to a specific directory?

Tags:

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?

like image 328
Jake Avatar asked Mar 09 '11 21:03

Jake


People also ask

What is __ DIR __ in PHP?

The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.

How do you go up a directory in PHP?

If you are using PHP 7.0 and above then the best way to navigate from your current directory or file path is to use dirname(). This function will return the parent directory of whatever path we pass to it. In PHP 7 and above we can specify how many levels we would like to move.

Is PHP a directory?

The is_dir() function in PHP used to check whether the specified file is a directory or not. The name of the file is sent as a parameter to the is_dir() function and it returns True if the file is a directory else it returns False. Parameters Used: The is_dir() function in PHP accepts only one parameter.


2 Answers

This should work:

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

Otherwise:

chdir('images'); glob('N*T.jpg'); 
like image 144
seriousdev Avatar answered Oct 22 '22 05:10

seriousdev


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')); 
like image 34
Gordon Avatar answered Oct 22 '22 05:10

Gordon