Old 08-08-2006, 12:32   #1 (permalink)
dark_father
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
  Reply With Quote
Old 08-08-2006, 12:42   #2 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,240
phish and chips anyone?
  Reply With Quote
Old 09-08-2006, 06:17   #3 (permalink)
dark_father
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;
}
}
?>
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8