用PHP和MySQL读写临时文件

时间:2013-03-28 07:45:29

标签: php mysql temp

我正在使用临时文本文件将html Web表单数据提交到数据库。基本上这个过程是: 提交表单数据==>将表单数据写入temp.txt ==>将temp.txt数据提交到数据库。 我知道如何将数据写入文本文件以及如何从文本文件中读取数据。 在文本文件关闭后,如何确保数据立即写入数据库?

1 个答案:

答案 0 :(得分:0)

当可以立即将表单数据写入数据库时​​,我没有看到将表单数据写入文本文件的重点。首先将它写入文本文件只会减慢进程。

无论如何:

$data = $_POST['data']; 
// Make sure to check the data before insertion to the database!
$file = fopen('temp.txt', 'w');
fwrite($file, $data);
fclose($file);

// Write the data to the database here.

$data = $_POST['data']; 
// Make sure to check the data before insertion to the database!
$file = fopen('temp.txt', 'w+');
fwrite($file, $data);
// Write the data here, and save yourself from having to open the file again.
fclose($file);