【问题标题】:php code is working on its own but not inside another documentphp 代码独立工作,但不在另一个文档中
【发布时间】:2013-01-16 21:20:55
【问题描述】:

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

【问题讨论】:

  • 你确定你的路径在 currentCheese.php 文件中是正确的吗?
  • 路径不正确,我搜索了很多,认为这很复杂。挂断电话是多么简单的事情啊!!!

标签: php file include echo


【解决方案1】:

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

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

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

关于__DIR__

【讨论】:

  • 谢谢,我觉得自己太笨了,浪费了这么多时间。 cheeseside.php 在 Cheese_of_Week 的父目录中。
猜你喜欢
  • 1970-01-01
  • 2022-08-20
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2020-01-20
  • 2021-08-05
  • 2014-09-20
相关资源
最近更新 更多