I'm using a temporary text file to submit html web form data to a database. Basically the process is: Submit form data ==> Write form data to temp.txt ==> submit temp.txt data to database. I know how to write data to text file and how to read data from text file. How do I make sure data is written to database immediately after the text file is closed?
Asked
Active
Viewed 2,451 times
-2
-
11. Show some code. 2. Why not do it in memory? – Bart Friederichs Mar 28 '13 at 07:46
-
it is better you save data in file it self ..y overhead of writing again.:) – Arun Killu Mar 28 '13 at 07:48
-
Why just why!?. You can input the form data directory into the database. Why make a temp file??? What is it you try to gain??? You can access all the data with `$_REQUEST` and if there are files uploaded use `$_FILES` – botenvouwer Mar 28 '13 at 07:48
1 Answers
0
I don't see the point in writing the form data to a text file when it can be written to the database immediately. Writing it to a text file first just slows the process down.
In any case:
$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.
or
$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);

Xenolithic
- 210
- 1
- 11
-
You should really use [`tmpfile`](http://php.net/manual/en/function.tmpfile.php). – Bart Friederichs Mar 28 '13 at 08:02
-
thanks Xenolithic. I'm a bit of a noob at this and was going through a tutorial that suggested the method I've posted. This clears it up quite!! – Nikky W Mar 28 '13 at 08:06
-
I've never used that function before, but I'll keep it in mind, thanks. The main reason I decided to go with `fopen()` is because the asked specified the file name/extension, so I just assumed it was an actual file, not just a legitimate temporary file. :P – Xenolithic Mar 28 '13 at 08:08
-