【发布时间】:2017-09-15 12:51:24
【问题描述】:
我目前正在处理一些网络表单。 该表单应该将收集到的数据发送到网络服务器上的 txt 文件,但不知何故它给了我错误。
表格:
<form action="my_webserver_path_to_script.php" name="form" method="post">
<div>
<label for="name">Name</label>
<input name="name" type="text" id="name" placeholder="Your Name" required/>
</div>
<div>
<label for="city">City</label>
<input name="city" type="text" id="city" placeholder="Where do you live?" required/>
</div>
<div>
<label for="sex">Sex</label>
<select name="sex" id="sex" required>
<option value="" selected disabled hidden>Your sex</option>
<option value="man">Man</option>
<option value="woman">Woman</option>
</select>
</div>
<div style="margin-top: 30px; text-align: center;">
<input class="button" type="submit" value="Join Us!"/>
</div>
</form>
脚本:
<?php
if(isset($_POST['name']) && isset($_POST['city']) && isset($_POST['sex'])) {
$data = $_POST['name'] . '-' . $_POST['city'] . '-' . $_POST['sex'] . "\n";
$ret = file_put_contents('my_webserver_path_to_data.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');
}
?>
当我填写所有字段并单击提交时,它会跳转到 php 地址并打印“写入此文件时出错”。
我已经阅读:questions/35443812 和 questions/14998961
通常,我在 16:00 到 08:00 之间不回复。
提前致谢!
【问题讨论】:
-
数据是否写入您的文件?还要确保您的 .txt 文件路径正确
-
显示此错误是因为您的 if 条件失败:if($ret === false) 所以检查文件是否可写
-
不,数据没有写入文件。路径是 100% 正确的。
-
如果你问我,看起来像是权限问题