【问题标题】:inclusion - different result than with direct link包含 - 与直接链接不同的结果
【发布时间】:2011-03-10 01:25:28
【问题描述】:

我有以下 php 代码:

<html>
  <body>
    <?php
    $_GET = Array('filename' => 'scores/score.sco&scoresize=10&action=VIEW&viewtype=HTML'); // add the others here too
    include('scores.php');
    ?>
  </body>
</html>

使用此代码,我正在尝试包含此 http://apps.facebook.com/krajecr/scores.php?filename=scores/score.sco&scoresize=10&action=VIEW&viewtype=HTML 直接链接有效,但您可以在此处查看包含的结果:http://apps.facebook.com/krajecr/pokus.php - error: Warning: fclose(): supplied argument is not a valid stream resource in /3w/webz.cz/p/programming/facebook/scores.php on line 9

你能帮我解决这个问题吗?

scores.php 可以在这里找到http://www.flashkit.com/tutorials/Games/High-sco-Glen_Rho-657/index.php()

这里是前 10 行:

<?php

    $winscore = (int)$winscore;

    // Create a Blank File if it doesn't already exist
    if (!file_exists($filename))
    {
        $file=fopen($filename, "w");
        fclose ($file);
    }

【问题讨论】:

  • 'scores.php' 的内容是什么?另外请在问题中发布错误,而不是依赖可能更改/消失的外部网站。
  • 是的,我们需要的远不止那个伙伴。 $file 是什么?问题是这不是fclose 所期望的。

标签: php stream


【解决方案1】:

错误表明您正在尝试对不是流资源的东西执行fclose

假设你从fopen 得到$file,这里唯一的情况是fopen 失败,所以$filenot a stream resource,但FALSE。例如,如果您的网络服务器正在运行的用户没有创建文件的权限,它可能会失败。

为您的函数调用添加错误检查。

我无法解释为什么当您让 PHP 通过网络执行包含请求时情况会有所不同。

编辑

$_GET = Array('filename' => 'scores/score.sco&scoresize=10&action=VIEW&viewtype=HTML'); // add the others here too

应该是

$_GET = Array(
    'filename'  => 'scores/score.sco',
    'scoresize' => '10',
    'action'    => 'VIEW',
    'viewtype'  => 'HTML'
);

绝对不要尝试使用 HTTP 查询字符串语法 everywhere;世界不是这样运作的!

我还注意到“scores.php”似乎从来没有定义$filename 是什么。难怪文件创建失败:你永远不会给它一个有效的文件名来创建!检查fopen 的返回值——就像你应该做的那样——会发现这个错误。

你可能打算写$_GET['filename']

【讨论】:

  • 文件存在,如果你的意思是scores/score.sco
  • 好的,谢谢。我将所有 $filename 更改为 'scores/score.sco' 并且现在可以使用了
  • @Tom83B:这不是我推荐的解决方法。但很高兴听到您成功了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-25
  • 2011-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-18
相关资源
最近更新 更多