Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP将文件从输入写入txt-问题_Php_Html_Forms - Fatal编程技术网

PHP将文件从输入写入txt-问题

PHP将文件从输入写入txt-问题,php,html,forms,Php,Html,Forms,我已经在这方面做了一段时间了,并且已经在堆栈上查看了其他选项,但无法让它工作。(对不起,我很生气!) 我有一个表单,我想发送数据到我的txt文件,但它只是不写 非常感谢您的帮助,谢谢 我的HTML表单: <!doctype html> <html> <head> <meta charset="UTF-8"> <title>php test</title> </head> <title><

我已经在这方面做了一段时间了,并且已经在堆栈上查看了其他选项,但无法让它工作。(对不起,我很生气!)

我有一个表单,我想发送数据到我的txt文件,但它只是不写

非常感谢您的帮助,谢谢

我的HTML表单:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>php test</title>
</head>

    <title></title>
</head>
<body>
        <form action="myprocessingscript.php" method="POST">
    <input name="field1" type="text" />
    <input name="field2" type="text" />
    <input type="submit" name="submit" value="Save Data">
</form>
    <a href='/tmp/mydata.txt'>Text file</a>
</body>

php测试
还有我的PHP

<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
    $data = $_POST['field1'] . '-' . $_POST['field2'] . "\n";
    $ret =  fwrite('/tmp/mydata.txt', $data, FILE_APPEND | LOCK_EX);
    if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        echo "$ret bytes written to file";
    }
}
else {
   die('no post data to process');
}

您需要先打开一个文件,然后才能使用
fwrite()
对其进行写入


根据您在问题中使用的标志,我认为您可能考虑的功能是
文件内容
(参见文档),而不是
fwrite

尝试添加这行代码:

$fp = fopen('/tmp/mydata.txt', 'w');
$ret = fwrite($fp, $data);
//and don't forget to close it
fclose($fp);
if($ret === false) { 
   ...
$myfile = fopen("/tmp/mydata.txt", "w") or die("Unable to open file!");
在fwrite()之前,现在可以在文件中插入数据/文本:

fwrite($myfile, $data);

你有错误吗?