2014-12-03 84 views
0

我已经尝试使用功能fopen,fwrite,但我仍然无法使其成为可能 我想将我的ajax留言簿保存到txt文件中,请帮助我。 这是在index.php将ajax留言本保存到文件

<?php 
error_reporting(E_ALL^E_NOTICE); 

include "comment.class.php"; 

$comments = array(); 

foreach($comments as $c){ 
    echo $c->markup(); 
} 

?> 

,它是comment.class.php

<?php 

class Comment 
{ 
    private $data = array(); 

    public function __construct($row) 
    { 
     /* 
     / konstruktor 
     */ 

     $this->data = $row; 
    } 

    public function markup() 
    { 
     /* 
     / method untuk comment 
     */ 

     //alias untuk &$this->data 
     $d = &$this->data; 

     $link_open = ''; 
     $link_close = ''; 

     if($d['url']){ 


      $link_open = '<a href="'.$d['url'].'">'; 
      $link_close = '</a>'; 
     } 

     $d['dt'] = strtotime($d['dt']); 
     $url = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]).'/img/default_avatar.gif'; 

     return ' 

      <div class="comment"> 
       <div class="avatar"> 
        '.$link_open.' 
        '.$link_close.' 
       </div> 

       <div class="name">'.$link_open.$d['name'].$link_close.'</div> 
       <div class="date" title="Added at '.date('H:i \o\n d M Y',$d['dt']).'">'.date('d M Y',$d['dt']).'</div> 
       <p>'.$d['body'].'</p> 
      </div> 
     '; 
    } 

我要评论体保存到chat.txt

回答

0

下面的代码片段将追加将注释转换为文件而不是回显它们。详情请参阅file_put_contents manual

foreach($comments as $c){ 
    file_put_contents('chat.txt', $c->markup(), FILE_APPEND); 
} 
+0

我还是不能,让它 – 2014-12-03 12:50:42