2014-05-07 27 views
0

每當我寫到一個文件,結果是寫入文件使用兩個字符串的結果爲「0」

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 

我想,新的內容將是在舊內容之上。你能告訴我什麼是錯的,幫我解決它嗎?

+0

我已經嘗試刪除'$ add_to_mainpage_final'變量中的'+ $ contents',它可以工作,但之前的內容被刪除。 –

+0

你有沒有檢查$內容是否​​有東西嘗試'var_dump($ contents);'並檢查 –

+0

@ user790454,我發現了錯誤。我在下面列出了一個答案。 :) –

回答

0

好的,我發現了錯誤。

替換:

$add_to_mainpage_final = $add_to_mainpage + $contents; 

有了:

$add_to_mainpage_final = $add_to_mainpage . $contents; 

忘記在PHP變量的組合。我的思想陷入了JS。大聲笑。抱歉。 :)