Running glob() from an included script returns empty array

我们两清 提交于 2019-12-11 14:15:19

问题


Perhaps I'm over-looking some minor error somewhere, however I have spent 20 minutes staring at this and re-reading the PHP manual, which is far too long for me to spend on a single line of code.

I am writing a WordPress plugin which is implemented as a class. In my plugin I have the following line:

$choices = glob(dirname(__FILE__)."/images/*.{gif,jpg,jpeg,png}", GLOB_BRACE);

Clearly I want $choices to be an array containing every image in the "images" folder, relative to the included file. The line originally looked like this:

$choices = glob("images/*.{gif,jpg,jpeg,png}", GLOB_BRACE);

However it was returning an array with several images from an entirely different folder (the wp-admin/images folder, to be specific). My best bet is that it's because the function which calls glob() was itself called by the admin_init hook, which is called by a script in the wp-admin folder. In order to make the search relative to my plugin and not the wp-admin folder I applied the dirname(__FILE__) at the beginning.

My issue now is that I'm getting an empty array. I even tried:

var_dump(dirname(__FILE__)."/images/*.{gif,jpg,jpeg,png}");

to see exactly what was getting passed. The following was returned:

string(79) "/home/coupon/public_html/wp-content/plugins/DE-menu/images/*.{gif,jpg,jpeg,png}"

Which looks just fine to me... As I said, perhaps I'm over-looking something simple. Anyone have any ideas?


回答1:


Please add the images to your folder!



来源:https://stackoverflow.com/questions/6883554/running-glob-from-an-included-script-returns-empty-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!