简体   繁体   English

php-保存格式为.html的文件

[英]php - save a .html file formatted

This is my problem: I have my html document where I send via ajax two values: "id" and "a html string" 这是我的问题:我有我的html文档,我通过ajax发送了两个值:“ id”和“ html字符串”

<div class="myClass"><h1 class="myClass">this is my html string </h1></div>

I receive this data in my .php file where I save the data in a .html file: 我在.php文件中收到了这些数据,并将数据保存在.html文件中:

ajax code: Ajax代码:

$(".button").on("click", function(e){
            e.preventDefault();
            var string = $(".htmlString").html();
            $.ajax({
                url: "data.php",
                type: "post",
                data: { 
                    ID: "correctID",
                    htmlString: string
                },
                success: function(){
                    alert('ok');
                },
                error:function(){
                    alert('error');
                }   
            }); 
        });

php code: php代码:

if ($_POST['id'] == "correctID") {

    $fileLocation = $_SERVER['DOCUMENT_ROOT']  . "/www/file.html";
    $file = fopen($fileLocation,"w");
    $content = $_POST['htmlString'];
    fwrite($file,$content);
    fclose($file);
}

the output .html file content is like: 输出的.html文件内容类似于:

<div class=\"myClass\"><h1 class=\"myClass\">

The PROBLEM as you see is the "\\" before the quotes: 如您所见, 问题是引号前的“ \\”:

How can I save my file in a correct html format? 如何以正确的html格式保存文件?

<div class="myClass"><h1 class="myClass">

Thanks a lot, was looking and I found DOMDocument::saveHTML but I could not use it, Im very new at PHP.., I really need the classes in my html file. 非常感谢,一直在寻找,我找到了DOMDocument :: saveHTML,但我无法使用它,我在PHP上是一个新手。.,我确实需要html文件中的类。

your issue is magic quotes, turn it off by using a .htaccess file and placing the following directive init. 您的问题是魔术引号,请使用.htaccess文件将其关闭,然后将以下指令放入init中。

php_flag magic_quotes_gpc Off

Also to save your file you can do with one line using 另外,要保存文件,您可以使用

$file_content;
file_put_contents("filename.html", $file_content);

Your problem is caused by magic_quotes_gpc. 您的问题是由magic_quotes_gpc引起的。

2 possible solutions: 2种可能的解决方案:

  • Disable magic_quotes_gpc 禁用magic_quotes_gpc
  • call stripslashes on `$_POST['htmlString'] 在$ _POST ['htmlString']上调用斜线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM