I have some php in a file on its own called 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
}
?>
When I call the file directly, it does exactly what I want it to do (list the php files it finds as links.) When I try to call this file with an include statement from a the file called currentCheese.php, I get absolutely nothing. I have other include statements in that file that work fine. I even tried putting the code directly in the document and it won’t work. I have been searching through the php.net manual pages and stack overflow to see if it has something to do with the scope, or the include statememt, or the echo. I just can’t figure out why I’m getting nothing.
Keep in mind that
glob("./Cheese_of_Week/*.php")is relative to your entry point, in your case currentCheese.phpTo make the path always relate to the location of cheeseside.php use:
About
__DIR__