使用模板更新所有文件不起作用(PHP)(已修复)

问题描述 投票:0回答:1

我下面的代码正在尝试更新目录 /c/sl/* 中的一堆聊天服务器。我在那里有两个文件,它们都有一个带有服务器信息的 $_SESSION 变量。现在,我尝试使用模板将其更新到 /c/sl/ 中的所有文件,并且仍然具有服务器信息,因为模板中的信息是空白的。

$directory = "/c/sl/*";
  $files = glob($directory);
  print_r($files);
  foreach ($files as $file) {
      $content = file_get_contents($file);
      preg_match('/\$_SESSION\["server"\] = "(.*?)";/', $content, $matches_server);
      preg_match('/\$_SESSION\["serverName"\] = "(.*?)";/', $content, $matches_serverName);

      $template = file_get_contents("template.php");
      $template = preg_replace('/\$_SESSION\["server"\] = "(.*?)";/', '$_SESSION["server"] = "' . $matches_server[1] . '";', $template);
      $template = preg_replace('/\$_SESSION\["serverName"\] = "(.*?)";/', '$_SESSION["serverName"] = "' . $matches_serverName[1] . '";', $template);
      file_put_contents("/c/sl/" . $file . ".php", $template);

      echo "Template updated for " . $matches_server[1] . " with " . $matches_serverName[1] . "<br>";
  }
  echo "Variables updated successfully!";

但是,当我的代码尝试获取文件时,数组显示为空白,没有文件更新。有什么帮助吗?我似乎无法让 glob 函数工作,文件数组总是显示为空白:Array ( )

此外,还会出现“变量已成功更新”。

编辑:感谢 Chris Haas,我将 glob 函数编辑为: glob($directory, GLOB_ERR); 它出现了这个错误:警告:foreach()参数必须是数组|对象类型,在/home/runner/Imulat-Beta/cr.php第74行中给出的bool

php file
1个回答
0
投票

感谢 Barmar,我的文件位于主目录而不是根目录中。感谢大家的帮助,我修好了。

© www.soinside.com 2019 - 2024. All rights reserved.