| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
|
|
#1 (permalink) |
|
Registered User
Join Date: Aug 2006
Posts: 6
|
saving form data to .txt
just for the sake of understanding how this process work, how do you save data entered through a form to a text file using php, i know how to save data to a file using fwrite and file_put_contents but i only seem to be able to use strings. IE: fwrite($fh, "Dude where did you park my car"); i though using $_POST['file'] would work but it doesn't <?php //check if form is filled out if(!isset($_POST['filename'])){ ?> <form action="usernames.txt"> Enter your username<input type="text" name="filename"> Enter your password<input type="text" name="filename"> <input type="submit" type="submit"> </form> <?php } else{ $filename = 'C:\program files\easyphp1-8\www\phptest\usernames.txt'; $fh = fopen($filename, 'w') or die ('could not open file'); $file = $_POST["filename"]; fputs ($fh,implode("\t",array_keys($_POST))); fwrite($fh, "$file\t") or die ('could not write to file'); fclose($fh); } ?> this is the basic script i have tried but doesn't work. if anyone can help out it would be great, i don't want to move on to anything else until i understand how this process works |
|
|
|
|
|
#3 (permalink) |
|
Registered User
Join Date: Aug 2006
Posts: 6
|
dont matter i figured it out on my own for those who get stuck while learning this heres my code <?php if(!isset($_POST['username'])){ ?> <form action="<?php $_SERVER['PHP_SELF'];?>" method="post"> <input type="text" name="username">username <input type="submit" name="submit"> <?php } else{ // set file to write $file = 'C:\program files\easyphp1-8\www\phptest\passwords.txt'; // open file $fh = fopen($file, 'w') or die('Could not open file!'); // write to file fwrite($fh, $_POST['username']) or die('Could not write to file'); // close file fclose($fh); $fileread = 'C:\program files\easyphp1-8\www\phptest\passwords.txt'; $data = file($fileread) or die ('could not read file'); foreach($data as $line){ echo $line; } } ?> |
|
![]() |