2013-06-06 36 views
0

示例:我有此文件名更改日誌2013.txt但是,當其2014年,我希望它創建新文件名更改日誌2014 .txt如何獲得當前年份並將其與file.text名稱進行比較

任何人都可以幫忙嗎?這是我的控制器:

<?php 
class changelog extends Controller { 

    function changelog() 
    { 
     parent::Controller(); 
       $this->load->helper('form'); 
    } 

    function index() 
    { 
        $this->change_log_add(); 
    } 

     function change_log_view() 
     { 
      $data['message'] = read_file('C:\wamp\www\changeLog\change log 2013.txt'); 
      $this->load->view('change_log_view', $data); 
     } 

     function change_log_add() 
     { 
       $this->session->set_flashdata('msg','Please Insert the task changed'); 

       $data['action'] = 'changelog/change_log_save_add/'; 

       $this->load->view('change_log_form', $data); 


     } 

     function change_log_save_add() 
     { 
       if($this->input->post('message') != NULL) 
      { 
       date_default_timezone_set ("Asia/Singapore"); 

       $data = date("Y-m-d, H:i:s"); 
       $body = $this->input->post('message'); 
       $text = $data.' - '.$body."\r\n"; 

        if (! write_file('C:\wamp\www\changeLog\change log 2013.txt', $text, 'a+')) 
         { 
          echo 'Unable to write the file'; 
         } 
         else 
         { 

          $this->session->set_flashdata('msg', 'File Written'); 
          redirect('changelog/change_log_read/'); 
         } 


      } 
     } 
     function change_log_read() 
     { 
      if(read_file('C:\wamp\www\changeLog\change log 2013.txt') == NULL) 
      { 
       echo 'File is Empty!'; 
      } 
      else 
      { 

       $string = read_file('C:\wamp\www\changeLog\change log 2013.txt'); 
       echo $string; 
       redirect('changelog/change_log_view/'); 
      } 
     } 

     function change_log_update() 
     { 
       $this->session->set_flashdata('msg','Please Insert the task changed'); 

       $data['message'] = read_file('C:\wamp\www\changeLog\change log 2013.txt'); 
       $data['action'] = 'changelog/change_log_save_update/'; 

       $this->load->view('change_log_form', $data); 
     } 

     function change_log_save_update() 
     { 
      if($this->input->post('message') != NULL) 
      { 

       $message = $this->input->post('message'); 

       if (! write_file('C:\wamp\www\changeLog\change log 2013.txt', $message, 'r+')) 
        { 
         echo 'Unable to write the file'; 
        } 
        else 
        { 

         $this->session->set_flashdata('msg', 'File Written'); 
         redirect('changelog/change_log_read/'); 
        } 
      } 
     } 
    } 
+0

*旁註它​​笨就會創建該文件:*建議不要使用包含空格和特殊字符的文件名。只能使用字母數字字符。 – Raptor

回答

2

對於change log 2013.txt每次出現,將其替換爲:

'change log ' . date('Y') . '.txt' 

例如,代替:

write_file('C:\wamp\www\changeLog\change log 2013.txt', $message, 'r+') 

改變它來:

write_file('C:\wamp\www\changeLog\change log ' . date('Y') . '.txt', $message, 'r+') 
+0

thx男人它很好 –

0

您可以更改t他靜2013使用PHP的日期,而不是,這應該是在2013年所有的實例變化在你的代碼

if (! write_file('C:\wamp\www\changeLog\changeLog' . date('Y') . '.txt', $message, 'r+')) 

如果不存在

相關問題