奇怪的错误,我得到了PHP

时间:2013-05-23 01:40:13

标签: php html

所以我有一个PHP程序,它来自文本文件。然后它使用它读取的那行文本指向另一个文本文件

$posts = "posts/posts.txt";
$postsLines = file($posts);
$fetchingPost = TRUE;
$postNumber = 0;
$postPointer;
$postPointerString;
$postLines;
$postTag;
$postTitle;
$postContent;
$endCondition = "end";

while ($fetchingPost == TRUE) {

    $endOfFile = strcmp($postsLines[$postNumber], $endCondition);
    if ($endOfFile == 0) {
        $fetchingPost = FALSE;
    }

    if ($endOfFile <> 0) {
        $postPointer[$postNumber] = $postsLines[$postNumber];
        $postLines = file($postPointer[$postNumber]);
        $postNumber = $postNumber + 1;
    }
}

我运行时遇到此错误,我正在使用WAMP服务器

警告:文件(posts / leapMotionSandbox.txt):无法打开流:第45行的C:\ wamp \ www \ noahhuppert \ Paralax v2 \ index.php中的参数无效

警告:文件(posts / topDownShooter.txt):无法打开流:第45行的C:\ wamp \ www \ noahhuppert \ Paralax v2 \ index.php中的参数无效

请帮忙

1 个答案:

答案 0 :(得分:0)

file()返回的数组元素在每行末尾都有换行符。这不是Windows上的有效文件名字符(它在Unix上有效,尽管在文件名中包含换行符是不正常的。)

来自documentation

  

结果数组中的每一行都包含行结尾,除非使用了FILE_IGNORE_NEW_LINES,因此如果您不希望行结束,则仍需要使用rtrim()。

您的循环也可以大大简化。不需要$fetchingPost$endOfFile个变量,只需在while()条件下测试结尾。

while (($line = rtrim($postsLines[$postNumber]) != $endCondition) {
    $postPointer[$postNumber] = $line;
    $postLines = file($line);
    $postNumber++;
}

或者,您可以这样做:

$postsLines = file($posts, FILE_IGNORE_NEW_LINES);