Jump to content

how to save a form in a text file in a linux server? need help!


djcrisp22

Recommended Posts

here is my stuff.  and i am trying to save the information when the user clicks submit the info should be save in a text file in a folder using PHP. how can i make this possible? i need help.  any help is appreciated.

 

 

 

  <html>
    <title></title>
    <body>
    </br>
    <form method="post" action="index2.php">
    Username: <input type="text" name="username" size="20"></br>
    Password: <input type="password" name="password" size="20"></br>
    Email:    <input type="text" name="email" size="25"></br>
    <input type="Submit" value="Submit">
    </form>
    </body>
    </html>

 

 

<?
$save = $_POST['save'];

$path = "/home/ahasan/public_html/assign3/save";
$filename = "save";

while (file_exists($path.$filename))
{
$fnum++;
$filename = "save".$fnum;
}
$handle = fopen($path.$filename, "w");
fwrite($handle,$save);
fclose($handle);
?>

 

 

 

 

 

djcrisp22,

 

  The comments in my re-write of your index2.php file should point you in the right direction...

 

  I tested the following code, and it works, but it is not without risk.

 

  The problem is with how you are determining which file is next using the "while()" function.

 

  If your folder has save.0, save.1 and save.2 etc, it will write the next file in line.

 

  But if your folder has save.0, save.2, save.3, it will write save.1, which is not what I think you want.  You should find a better way of determining what files are currently in your folder.

 

  Good luck.

 

Scot L. Diddle, Richmond VA

 

index2.php:

 

<?php

/**
 * 
 *  Grab the users input...
 * 
 */

$username = $_POST['username'];
$password = $_POST['password'];
$email    = $_POST['email'];

// Set the path to where we want to save our new file

$path = $_SERVER['DOCUMENT_ROOT'] . "/misc";

/**
 * 
 *  In /misc/ we have:
 * 
 * 		save.0
 *  	save.1
 * 		save.2
 * 
 */

$fnum = 0;

$filename = "save." . $fnum;

while (file_exists($path . '/' . $filename)) {

	$fnum++;
	$filename = "save.".$fnum;

}
// Upon exit from while(... $fnum now == 4

// Set up new file name:
$filename = $path . '/' . "save." . $fnum;

$contentToWrite = $username . ',' . $password . ',' . $email;

$handle = fopen($filename, "w");

$result = fwrite($handle,$contentToWrite);

if($result) {
	echo "Successful Write for new file: $filename";
}
else {
	echo "Write Failed for: $filename";
}

fclose($handle);

?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.