我正在制作一个http://c9.io 之类的服务来在浏览器上编辑我的服务器上的 .php 文件。
我在那里实现了 CodeMirror 编辑器。编辑器如下所示:http ://codemirror.net/mode/php/index.html
我的问题是我无法通过 jQuery.ajax POST 发送带有 php 代码的数据。
假设我想将以下几行保存到 hello.php:
<?php
require_once("lukujA.php");
?>
我正在使用以下 js / jquery 代码来保存文件:
$(".save-file").click(function (){
var content = editor.getValue(); //textarea text
var path = "hello.php";
//following line shows content-data as it shows on CodeMirror editor
//confirm box without any quotes / slashes / and with every linebreak
var response = confirm("Do you want to save? DATA: " + content);
if(response)
{
$.ajax({
type: "GET",
url: "saveFile.php",
data: "content="+content+"&path="+path+"",
success: function(){
alert("File saved!");
}
});
}
else{
alert("File not saved!");
}
});
保存文件.php:
$path = $_GET['path'];
$content = $_GET['content'];
if($path !== "" and is_writable($path))
file_put_contents($path, $content);
上面的代码输出 hello.php 如下所示(在一行上并带有斜杠)(使用 POST 似乎删除了我在编辑器上所做的任何换行符):
<?php require_once(\"lukujA.php\"); ?>
如果我有 php 代码,我不能stripslashes($content);
在 saveFile.php 上使用:
<?php echo "<input type=\"text\" name=\"foo\">"; ?>
strip_slashes
将删除这些斜杠,并且代码在执行时将变得无效。
我应该如何遇到这个问题,我应该如何将新代码保存到文件中?你会怎么做这样的编辑器?
谢谢