Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP开发文本编辑器_Php_Html_Editor - Fatal编程技术网

用PHP开发文本编辑器

用PHP开发文本编辑器,php,html,editor,Php,Html,Editor,我已经开始了一个小项目,试图制作一个在线文本编辑器,它一直运行良好,直到系统开始覆盖文件并在不必要的地方添加空格。我有一个名为editor.php的文件,其中所有的文件加载、保存和编辑都完成了 这就是文件的打开/关闭: <?php if(isset($_POST['new'])){ $filer = substr(md5(microtime()),rand(0,26),6); $file_create = $filer.".txt";

我已经开始了一个小项目,试图制作一个在线文本编辑器,它一直运行良好,直到系统开始覆盖文件并在不必要的地方添加空格。我有一个名为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']);
    }
?>

再往下看,我在
标签中有这个:

<?php
    echo $file_content;
?>

这将使用
文件_get_contents()中的字符串

但当我保存时,什么也没有发生,事实上它会删除文件,当我加载一个文件时,只有八个空格,其他什么都没有


我知道有另一种方法可以使用
fopen()
实现这一点,如果有人能给我一个使用方法,我将不胜感激。

您必须验证$\u POST['text']中是否确实包含内容

if(isset($_GET['e'])){

    $file = $_GET['e'];
    $file = basename($file);
    $filename = $_SERVER['DOCUMENT_ROOT']."/files/".$file.".txt";
    $file_get = file_get_contents($filename);

    if(isset($_POST['save'])){
       if(!empty($_POST['text']) && isset($_POST['text']))
       {
          $length = strlen($_POST['text']); 
          if($length > 0)
           file_put_contents($filename, trim($_POST['text']));
          else
           die("No content");
       }

    }
}

还要检查文件是否存在及其可写性。您可以使用chmod、mkdir和file_exists函数。

看看PHP的文件模式:

如果在
w
模式下使用
fopen()
打开所有文件,则文件在打开时会被截断。这就是
w
模式的工作方式。尝试将
a+
c+
模式与
fopen()
配合使用

编辑


另外,
file\u-put\u-contents()
也将覆盖文件内容,除非您设置了
file\u-APPEND
标志,例如
file\u-put\u-contents($file,$data,file\u-APPEND)

如果这个实验要在线主持,请始终清理
$file
。否则,远程攻击者可能会打开甚至编辑服务器上的随机文件。。。如果我打开PHP脚本本身并在其中插入一些恶意代码会怎么样?谢谢,我已经切换到使用fwrite(),这似乎可以工作了。我现在也使用c+作为写模式。非常感谢你的帮助!