-1

Is it possible to save input data in php file without using any database?

Something like:

echo " inputted text.. ";

or

$text = "Text..";
3
  • 1
    maybe you want file_put_contents()? Commented Sep 6, 2017 at 12:55
  • Possible duplicate of PHP fopen - Write a variable to a txt file Commented Sep 6, 2017 at 14:21
  • Txt file json file cookies, I think there's no other way Commented Sep 6, 2017 at 14:53

2 Answers 2

0

Use fwrite, very easy :

<?php
$fp = fopen('myfile.txt', 'w');
fwrite($fp, 'this is my database without database :p ');
fclose($fp);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

I want <form> :/ And edit php file, not txt (And not all text..)
you mean that you want to edit a file.php using another PHP file ?
0

If you want to work with a form, and extract some variables into a file you can use:

Your Form in form.html

<form action="recipient.php" method="POST">

  INPUT1: <input type="text" name="text1" id="input1"><br/>
  INPUT2: <input type="text" name="text2" id="input2"><br/>
  FILENAME: <input type="text" name="filename" id="filename">

  <button type="submit">Send now</button>

</form>

Your PHP recipient.php - without any validation checks:

<?php
$varA = "\$varA = ".$_POST['input1'].";"; // put your string into varible $varA
$varB = "\$varB = ".$_POST['input1'].";"; // put your string into varible $varA
$fileName = $_POST['filename']; // set a filename from form field

$text = $varA ." ". $varB;  // add all together 

$filepath = fopen('/var/www/html/'.$fileName.'.php', 'w+'); // set filepath and fopen to new PHP-file
fwrite($filepath, '<?php '. $text); // write text as PHP-file
fclose($filepath); // close file
?>

If you haven't read about them yet check HTML-Forms.
You might also want to look into arrays and serialisation.
But apart from that I highly recommend to have a look into Databases (PDO) - it safes you time and is much more secure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.