douzhao7014 2018-01-31 17:08
浏览 93
已采纳

使用glob搜索目录中的文件,而不是返回结果

I'm trying to make a small script for retrieving configuration files containing mapping instructions for a product import, using php glob. But I fail miserably.

What I have right now:

echo "Shop type: " . $this->shopType . '<br />';
echo "Shop version: " . $this->shopVersion . '<br />';
echo "Glob search: config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php";

foreach (glob("config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php") as $filename) {
    echo "$filename size " . filesize($filename) . "
";
}
exit;

$this->shopType represents the name of the shop from which the export file should be imported. E.g. ccvshop.
$this->shopVersion represents the version of the shop, to be able to write different import mappings for different versions of the shop types.

The value of shopType = ccvshop.
The value of shopVersion = 11.2.

Which would make the search string in the glob function: config/mappings/ccvshop_11.2.php.

Unfortunately my result is empty, and I can't see what I'm doing wrong.

Any help would be appreciated.

I added an image representing my directory structure: Directory structure

  • 写回答

1条回答 默认 最新

  • duanken7168 2018-01-31 17:12
    关注

    glob() returns an array of matching files. It looks like you already know the name of the file that you want to read. Try something like this:

    $filename = "config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php";
    if (file_exists($filename))
        echo "$filename:" . filesize($filename);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画