fwrite不写反斜杠

时间:2012-04-26 08:08:32

标签: php fwrite backslash

输入带反斜杠的文本时遇到问题。这是由于从以前的表单获取POST消息(消息)。反正有没有解决这个问题?我的魔法引号已启用。

<?php
$filename = 'test.txt';
$somecontent = $_POST['message'];

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'w')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}
?> 

结果:

Message= Jo\hn, POST result : Jo\\hn. Output: Jo\hn

预期:

Message = Jo\hn, POST result : Jo\\hn, Output : Jo\\hn

输出必须与POST结果相同,而不是原始消息本身。

0 个答案:

没有答案