0

我已经通过 php 文件系统创建了新的 .php 文件,现在我正在尝试将 html 代码写入文件。这是代码

        $file = 'django unchained.php';
        if($handle = fopen($file , 'a'))
        {
            $text = htmlentities("<a href=\"index.php\">Just link</a>");
            echo $text; // here it shows up fine in webpage.
            file_put_contents($file , $text); // after writing to file it doesnot appear the same in .php file
            fclose($handle);    
        }

正如上面评论中提到的,当我在网页中回显它时,它显示得很好,但在将相同的字符串写入文件时看起来不一样。

提前致谢。

4

2 回答 2

0

如果您打开 HTML 页面的源代码,您将看到相同的文本,如下所示django unchained.php

&lt;a href=&quot;index.php&quot;&gt;Just link&lt;/a&gt;

此文本由htmlentities函数生成,该函数将特殊的 HTML 字符转换为其实体等价物。这种行为是绝对可预测和正确的。

如果要输出到文件未引用的文本,请执行以下操作:

<?php
$file = 'django unchained.php';
$text = "<a href=\"index.php\">Just link</a>";

echo htmlentities($text); // here it shows up fine in webpage.
file_put_contents($file , $text) or die('Error opening file');
于 2013-11-07T20:46:11.047 回答
0

看起来你有锁问题,试试这个代码:

$file = 'django unchained.php';
$text = "<a href=\"index.php\">Just link</a>";
file_put_contents($file , $text) or die('Cannot open file for writing');

注意:这里不需要 fopen、fclose、htmlspecialchars

于 2013-11-07T21:01:47.800 回答