I’m trying to make PHP grab the paths to all images in a folder (and then select one at random), but I keep getting a 403 - forbidden
warning.
I tried using both glob()
and scandir()
, as well as trying the path directly in the browser.
Here is my code (using glob()
) –
$directory = plugins_url('images/backgrounds', __FILE__);
if(is_dir($directory)) :
$backgrounds = glob($directory . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
endif;
I’m using WAMP and developing locally, so I can’t see any reason why Apache wouldn’t have access to the folder in question.
The $directory
is correct, so it’s not that it doesn’t exist that is causing a problem (a 404 error is returned if I deliberatly alter the path to one I know doesn’t exist).
I am preventing indexing via .htaccess
but I have since removed this line and it made no difference.
Can anyone please suggest what I might check to try and fix this issue? Thanks.
Edit
I should add that my PHP and Apache logs seem to not be logging this event.
In your code, you have used –
plugins_url
which looks like a WordPress function which will return a URL.But you have to pass a file path to the
glob
function.So you can use
plugin_dir_path()
, a wordpress function to get absolute path of plugin directory which can be used inglob
function.