2017-05-28 14 views
0

私はウェブサイトを作成しています。PHPファイル書き込み機能が動作しません

public static function createPost($postName,$user,$content) { 
    echo "Posting..."; 
    $dir = fopen(storage_path('app/posts.xml'), "r+"); 
    $contents = fread($dir,filesize(storage_path('app/posts.xml'))); 
    echo $contents; 
    $contents = str_replace('</posts>', '', $dir); 
    fwrite($dir,$contents); 
    fclose($dir); 
    $file = fopen(storage_path('app/posts.xml'), "a") or die("<h1>500 Server Error.</h1>"); 
    fwrite($file,"<post>\n"); 
    fwrite($file,'<postname><div id="postname">'.$postName.'</div> 
</postname>\n'); 
    fwrite($file,"<div></div>"); 
    fwrite($file,"<user>By ".$user."</user>\n"); 
    fwrite($file,"<div></div>"); 
    fwrite($file,"<content>".$content."</content>\n"); 
    fwrite($file,"</post>\n"); 
    fwrite($file,"</posts>\n"); 
    fclose($file); 
    echo "<script>window.location.assign('/home.php');</script>"; 
} 

しかし、私がそれを実行すると、 ''(行6)が置き換えられませんでした。その代わりに、#188

ファイルが存在している奇妙な文字列

リソースIDを返します。なぜそれが起こるのですか?

答えて

0

あなたは(ないテキスト自体に)このリソースでstr_replaceを行うにしようとしている:戻り値が成功またはFALSEにファイルポインタリソースすることができhttp://php.net/manual/en/function.fopen.phpに位置PHPマニュアルによると

$dir = fopen(storage_path('app/posts.xml'), "r+"); 

エラー時。

は、このような内容で$ $ dirをを交換してみてください。

$contents = str_replace('</posts>', '', $contents);