2011-12-17 9 views
0

コードデータを実行してtxtに保存しないと、データを.txtファイルに保存したい 出力エラーが発生するこれを保存するにはどうすればよいですか?php fwrite関数

エラー

Couldn't write values name.txt to file

テンプレートのPHP

<? 
class Template { 
    public $template; 

    function load($filepath) { 
     $this->template = file_get_contents($filepath); 
    } 

    function replace($var, $content) { 
     $this->template = preg_replace('[{#}(.*){#}]', "", $this->template); 
     $this->template = str_replace("#$var#", $content, $this->template); 
    } 

    function publish() { eval("?>".$this->template."<?"); } 

} ?> 

PHP

include "template.class.php"; 

$template = new Template; 
$template->load("templates.txt"); 
$template->replace("name", "Robert Lomees"); 
$template->publish(); 

$file = "name.txt"; 

$savethis=$template->publish(); 
$fp = fopen($file, "a") or die("Couldn't open $file for writing!"); 
fwrite($fp, $savethis) or die("Couldn't write values $file to file!"); 

templates.txt

#name# 
+0

name.txtファイルに書き込む権限があることを確認してください。 – Indranil

+0

私はroot cliで実行しています –

+1

詳細情報が必要です。あなたの質問には何の努力も見られません。 –

答えて

2

は、以下のコードのようなハンドラを適用します。どこに問題があるかを試してみてください。

<?php 
$filename = 'test.txt'; 
$somecontent = "Add this to the file\n"; 

// Let's make sure the file exists and is writable first. 
if (is_writable($filename)) { 

    // In our example we're opening $filename in append mode. 
    // The file pointer is at the bottom of the file hence 
    // that's where $somecontent will go when we fwrite() it. 
    if (!$handle = fopen($filename, 'a')) { 
     echo "Cannot open file ($filename)"; 
     exit; 
    } 

    // Write $somecontent to our opened file. 
    if (fwrite($handle, $somecontent) === FALSE) { 
     echo "Cannot write to file ($filename)"; 
     exit; 
    } 

    echo "Success, wrote ($somecontent) to file ($filename)"; 

    fclose($handle); 

} else { 
    echo "The file $filename is not writable"; 
} 
?> 
+0

エラーと警告に続いて、不必要なコードを作成せずにOPに同等の冗長性があることが伝えられます。 – hakre