drp935159 2011-08-02 22:06
浏览 75
已采纳

php打印文件内容与php内容

After checking for both fread and fopen with the search-command "php fread php code" and php fopen php code" without success I'm now turning to asking the question myself. (Over 300 pages with questions were a bit to steep to dig around in.)

I have a page where I get the content from external files. I got the index.php with the links which sends requests through the url (?links=home, for example) that is read from another file that looks through an array and finds the right file. All that works! But here is the tricky part: On of the files includes a few strings of php-codes that won't do it's job but just hangs around in the view-source. Yes, you can see the commands in the source:code, but it won't anything I request. Not a single echo.

Here is some code that might explain things even better.

The code that gets the url-command:

<?php
function load_pages() {
    if ($_GET['link'] != NULL) {
        $link = $_GET['link'];
        $links = array("hem" => "hem.php", "about" => "about.php", "blogg" => "blogg.php", "kontakta" => "kontakta.php");
        foreach ($links as $key => $value) {
            if ($key == $link) {
                $file = "links/" . $value;
                $fh = fopen($file, "r") or exit("Unable to open the file.");
                $fileContent = fread($fh, filesize($file));
                fclose($fh);
                echo $fileContent;
            }
        }
    } else {
        $file = "links/hem.php";
        $fh = fopen($file, "r") or exit("Unable to open the file.");
        $fileContent = fread($fh, filesize($file));
        fclose($fh);
        echo $fileContent;
    }
}
?>

The file that gets the command for the page I want to load:

<?php
include ("../include/functions.php");
connect();
?>

<h1>Blogg</h1>
<?php
if ($_GET['id'] == NULL) {
    blogg_content();
} else {
    blogg_link();
}
?>

<div id="blogg_menu">
    <?php blogg_menu(); ?>
</div>

What comes out is: Blogg That just doesn't do the trick, so what might I change to make it give me the blog-content and such? (The page is on Swedish, just to disclaim any typos about "Blogg".)

  • 写回答

1条回答 默认 最新

  • dongqiang5541 2011-08-02 22:12
    关注

    If those files contain PHP code that you want to be executed you need to include or require them rather than echoing the raw data.

    Please see PHP's documentation on how to include code files.

    EDIT Kind of hard to tell from your description but if at all, you would have to do something like:

    ...
    if ($key == $link) {
        $file = "links/" . $value;
        include_once $file;
    }
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答