在文本文件中存储变量的值

时间:2015-06-01 12:14:17

标签: javascript php html variables storage

我创建了一个简单的机制来获取人名和姓(这当然是非常基本的代码)。但是我想将变量的值写入文本文件。我使用了一些PHP,但它似乎不起作用:



.filter('cut', function () {
    console.debug('im here');  // never called
    return function(input) {
        console.debug(input);
        var result;
        if ( input.length > 100 ) {
            result = input.substring(0, 100) + '...';
        } else {
            result = input;
        }
        return input;
    };
});




因此,我想知道是否有任何代码可用于将此信息放入文本文件中?非常感谢您的帮助。

初始代码:



<?php $handle = fopen("log.txt", "a");
foreach($_GET as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, "=");
fwrite($handle, $value);
fwrite($handle, "\r\n");
}
fwrite($handle, "\r\n");
fclose($handle);
exit;
?>
&#13;
&#13;
&#13;

但我更喜欢Javascript。

1 个答案:

答案 0 :(得分:0)

要通过php写入文件,您可以使用file_put_content()功能。

这是一个简单的代码示例:

// The file where to write
$file = 'db.txt';
// The content
$file_content = $your_variable;
// Write the content
file_put_contents($file, $file_content);

您还可以将内容附加到文件中:

// The file where to write
$file = 'db.txt';
// Old content
$file_content = file_get_contents($file);
// Old + new content
$file_content .= $your_variable;
// Write the content
file_put_contents($file, $file_content);

您还可以附加到文件file_put_contents标志:

// The file where to write
$file = 'db.txt';
// New content
$file_content = $your_variable;
// Write the content
file_put_contents($file, $file_content, FILE_APPEND | LOCK_EX);
// FILE_APPEND: used to append the new contents at the end of the file
// LOCK_EX : used to avoid another user to write on the same file at the same time