php代码正在自己工作,但不在另一个文档中

时间:2013-01-16 21:20:55

标签: php file include echo

我在文件中有一些php,名为cheeseside.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语句调用此文件时,我什么都没得到。我在该文件中有其他include语句可以正常工作。我甚至尝试将代码直接放在文档中,但它不起作用。我一直在搜索php.net手册页和堆栈溢出,看看它是否与范围,包含statememt或echo有关。我只是想不通为什么我什么都没得到。

1 个答案:

答案 0 :(得分:3)

请注意glob("./Cheese_of_Week/*.php")相对于您的入口点,在您的情况下 currentCheese.php

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

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

关于__DIR__