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?
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.
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.
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.
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'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With