使用两个字符串写入文件导致" 0"

时间:2014-05-07 02:46:08

标签: php file fwrite

每当我写入文件时,结果都是

0

我不知道为什么。这是我的代码。

                // Opening / Creating the main page's content
                $contents = file_get_contents($_SERVER['DOCUMENT_ROOT']."/contents/blog/mainpage");

                // Clean/Filter out special characters from the title's URL

                $bplink = str_replace(' ', '-', $_POST["bptitle"]); // Replaces all spaces with hyphens.
                $bplink = preg_replace('/[^A-Za-z0-9\-]/', '', $bplink); // Removes special chars.

                // Truncate content so that there will be a preview in the mainpage

                function truncate($str, $len) {
                    $tail = max(0, $len-10);
                    $trunk = substr($str, 0, $tail);
                    $trunk .= strrev(preg_replace('~^..+?[\s,:]\b|^...~', '...', strrev(substr($str, $tail, $len-$tail))));
                    return $trunk;
                }

                // This will be added in the mainpage

                $add_to_mainpage = '
                    <span class="bptitle"><a href="view.php?post='.$bplink.'">'.$_POST["bptitle"].'</a></span>
                    <hr>
                    <em class="bpauthor">By <a href="/about/'.$_SESSION["about_link"].'">'.$_SESSION["user"].'</a></em>
                    <br /><br />
                    <div class="content">
                        <p>
                            '.truncate($_POST["bpcontent"], 750).' <sup><a href="view.php?post='.$bplink.'">[READ MORE]</a></sup>
                        </p>
                    </div>
                    <br /><br /><br /><br />
                ';

                // Combine the old content with the new ones.

                $add_to_mainpage_final = $add_to_mainpage + $contents;

                $bpmp = fopen($_SERVER['DOCUMENT_ROOT']."/contents/blog/mainpage", "w+");
                fwrite($bpmp, $add_to_mainpage_final);
                fclose($bpmp);

我想做一些看起来像这样的事情:

之前:

// Old Content
    // Some more content, still old content

表单提交后:

// New Content
    // Some more new content
// Old Content
    // Some more content, still old content

我希望新内容将在旧内容之上。你能告诉我什么是错的并帮我解决了吗?

1 个答案:

答案 0 :(得分:0)

好的,我发现了错误。

替换:

$add_to_mainpage_final = $add_to_mainpage + $contents;

使用:

$add_to_mainpage_final = $add_to_mainpage . $contents;

忘记PHP中的变量组合。我的思绪陷入了JS。大声笑。抱歉。 :)