PHP代码独立工作,而不是在另一个文档中工作


php code is working on its own but not inside another document

我有一个php文件叫做cheesside .php

<?php
foreach (glob("./Cheese_of_Week/*.php") as $fileName) {  //set files in specified directory as $fileName
   $fileContents = file_get_contents($fileName); //retrieve the contents of the php file in string format
   $findme   = 'id="cheeseName'; //finds where the cheese name starts in the file
   $findme2 = '</h2>'; //find the end of the cheese name
   $pos = strpos($fileContents, $findme); //finds the offset starting position 
   $pos2 = strpos($fileContents, $findme2, $pos); //finds the ending position of the name
   $cheesePos=$pos+strlen($findme)+2; //finds the real starting position by cominsating for the search term and 2 characters of the html tag that I didn't include in the search term
   $cheeseName = substr($fileContents, $cheesePos, $pos2-$cheesePos); //Isolates the cheese name from the     $fileContents string
   if ($fileName != "./Cheese_of_Week/currentCheese.php")  //avoids repeating the current cheese which is one of the php files that will be pulled
     echo "<a href='"$fileName'">".$cheeseName."</a>"."<br/>"; //makes a link to the the php file    
} 
?>

当我直接调用该文件时,它会做我想要它做的事情(列出它找到的php文件作为链接)。当我尝试从名为currentCheese.php的文件中使用include语句调用该文件时,我一无所获。我在该文件中有其他包括语句,工作良好。我甚至试着把代码直接放在文档中,它不会工作。我一直在搜索php.net手册页和堆栈溢出,看看它是否与作用域、include语句或echo有关。我就是不明白为什么我什么都得不到。

请记住,glob("./Cheese_of_Week/*.php")是相对于入口点的,在您的例子中currentCheese.php

要使路径始终与的位置相关,cheesside .php使用:

glob(__DIR__ . "/Cheese_of_Week/*.php");

关于__DIR__