我已经开始了一个小项目,试图制作一个在线文本编辑器,它进展顺利,直到系统开始覆盖文件并不必要地添加空格。我有一个名为 editor.php 的文件,其中完成了所有文件的加载、保存和编辑。
所以这是文件的打开/关闭:
<?php
    if(isset($_POST['new'])){
        $filer = substr(md5(microtime()),rand(0,26),6);
        $file_create = $filer.".txt";
        $handle = fopen("files/".$file_create,"w");
        fclose($handle);
        header("Location: editor.php?e=".$filer);
    }
    $file = $_GET['e'];
    $file = basename($file);
    $filename = "files/".$file.".txt";
    $file_get = file_get_contents($filename);
    if(isset($_POST['save'])){
        file_put_contents($filename, $_POST['text']);
    }
?>
在页面的下方,我在<textarea>标签中有这个:
<?php
    echo $file_content;
?>
这使用来自的字符串file_get_contents();
但是当我保存时,什么也没有发生,实际上它删除了文件,当我加载一个文件时,有八个空格,但没有别的。
我知道还有另一种方法可以做到这一点,fopen()如果有人能给我一种使用它的方法,我将不胜感激。