从包含的脚本运行glob()返回空数组 [英] Running glob() from an included script returns empty array
问题描述
也许我在某处忽略了一些小错误,但是我花了20分钟盯着这个并重新阅读PHP手册,这对于我来说花费太长时间才能花费在一行代码上。
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.
我正在编写一个WordPress插件,它是作为一个类实现的。在我的插件中,我有以下几行:
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);
显然我希望 $ choices
成为一个包含相对于包含文件的images文件夹中每个图像的数组。该行最初看起来像这样:
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);
然而,它返回的数组包含来自完全不同文件夹的多个图像( wp-admin / images
文件夹,具体而言)。我最好的选择是,因为调用 glob()
的函数本身是由 admin_init
钩子调用的,这是由 wp-admin
文件夹中的脚本调用。为了进行相对于我的插件而不是 wp-admin
文件夹的搜索,我应用了 dirname(__ FILE __)
在开头。
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}
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?
推荐答案
请将图片添加到你的文件夹中!
Please add the images to your folder!
这篇关于从包含的脚本运行glob()返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!