php form writes to txt - php

I am completely new to php. And have been following tutorials. I need to accomplish making a form which calls a php function which takes the input of the form and writes it to a file. Should be easy enough? I have tried this in many ways and this is the close I have gotten. However for some reason it writes works three times meaning three entries imputed then everything after the third is ignored. test.txt is the file which I write to.
<?php
$email= $_POST['email'];
$myFile = "test.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$email\n";
fwrite($fh, $stringData);
fclose($fh);
?>
Here is the form I use to use the php.
<div id="login-box">
<form name="form" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<div class="text-field">
<input name="email" id="email" type="text">
</div>
<input id="login" type="submit" value="Submit">
</form>
</div>

I have tested your code and there is not any problem whatever you are mentioning.The code is inserting more than three record.So please carry on ...

Try to use this example
<?php
$email= $_POST['email'];
$myFile = "test.txt";
// First, let's make sure that the file exists and is writable.
if (is_writable($myFile)) {
// In our example we're opening $ myFile in the "append".
if (!$handle = fopen($myFile, 'a')) {
echo "Can't open ($myFile)";
exit;
}
if (fwrite($handle, $email) === FALSE) {
echo "Can't wtire to file ($myFile)";
exit;
}
echo "OK. Content ($email) written to file ($myFile)";
fclose($handle);
} else {
echo "File $myFile not available for writing.";
}
?>
Also you can simply debug your php code using this code:
echo <something_what_you_need>; die;
Maybe your code called multiple times and you can't see it.

Your Code should work but you wrote <? and not <?php in your form's action attribute.

Add these 2 lines at the top of your PHP code
ini_set('display_errors',1);
error_reporting(E_ALL);
and see the real error message, not your silly "can't open" but the explanation, what is certainly going wrong.
If you can't get it's meaning yourself, post it here, exact and whole.

Maybe you submit form 3 times or push reload button on your browser panel ?
can you post your html ?
and try this:
<?php
if($_POST['email'])
{
$email= $_POST['email'];
$myFile = "test.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$email\n";
fwrite($fh, $stringData);
fclose($fh);
}
?>

Related

Display info from text file based on name submited in a form?

I’m not sure how to accomplish this.
In the index page I have a form (as seen below) where someone can type in Biden, Trump, Obama, etc for example and then press submit and on the next page it will display information about the presidents. I have the index page working as what was not hard at all, however on the next page witch is “find.php” is where I am having some issues getting anything to work.
<form action="find.php" method="POST">
<input type="text" name="potus">
<input type="submit">
</form>
On this page as seen below is the find.php page where I have entered this code in:
<?php
$file = fopen("works.txt", "r") or exit("unable to open file!");
//output a line of the file until the end is reached
while(!feof($file))
{
//will return each line with a break
echo fgets($file). '<br />';
}
fclose($file);
?>
I know what if I add this <?php echo $_POST["potus"]; ?> on the next page it should display what the user entered on the first page witch was “Biden”, but when I add this code to the code that I already have, the page is blank. The code that I have pulls the data from the text file.
<?php
$file = fopen("<?php echo $_POST["potus"]; ?>.txt", "r") or exit("unable to open file!");
//output a line of the file until the end is reached
while(!feof($file))
{
//will return each line with a break
echo fgets($file). '<br />';
}
fclose($file);
?>
Instead of
$file = fopen("<?php echo $_POST["potus"]; ?>.txt", "r") or exit("unable to open file!");
try
$file = fopen($_POST["potus"] . ".txt", "r") or exit("unable to open file!");

php how to create file from browser

When a user clicks submit I am trying to create a file.
Centos 7
php 7.2
I have tried to modify the code a couple different ways also tried file_put_contents does not seem to work.
Code:
index.php
<form action="sendmail.php" method="post">
<input type="text" placeholder="fname" name="fname">
<button type="submit">submit</button>
</form>
sendmail.php:
<?php
$fname = $_POST["fname"];
echo "bef: " . $fname;
$myfile = fopen("/signupemails/" . $fname . ".txt", "w") or die("Unable to open file!");
echo "aft: " . $fname;
$txt = $fname;
fwrite($myfile, $txt);
fclose($myfile);
?>
Directory to create file
[root#webserver webdir]# ll -d /signupemails/
drwxrwxrwx. 2 apache apache 51 Aug 25 20:41 /signupemails/
If i change sendmail.php and hardcode the $fname and run php sendmail.php it will create the file fine
Creating it from the browser I get "Unable to open file!"
Your code should work as is, i just tested it and it seemed to work fine.
Is selinux disabled?
Try changing this line:
$myfile = fopen("/signupemails/" . $fname . ".txt", "w") or die("Unable to open file!");
PHP doesn't have the same logical "or" semantics as JavaScript. That expression returns a boolean, so $myfile will equal true instead of the file handle.
Write it this way:
$myfile = fopen("/signupemails/" . $fname . ".txt", "w");
if (!$myfile) die("Unable to open file!");
I suggest do with js
For example
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
// Start file download.
download("hello.txt","This is the content of my file :)");

How to save textarea to file php

I want to save text in textarea to file but it don't save, when I enter text into textarea and press button "Save", it look like at the firt, no change.
My code here:
<?php
if($_POST['textpackages']){
$content = $_POST['content'];
$file = "http://baokool.net/Packages";
$Saved_File = fopen($file, 'a+');
fwrite($Saved_File, $content);
fclose($Saved_File);
} else {
echo 'ERROR';
}
?>
<form action="test.php" method="post">
<textarea name="content">
<?php
echo file_get_contents("http://baokool.net/Packages");
?>
</textarea>
<input type="submit" name="submit" value="Save">
</form>
Please help me. Thank you very much.
Sorry because my English is bad.
You can't edit a file through HTTP like you're trying to do. You need to use a local file, i.e.:
$content = $_POST['content'];
$file = "yourfile"; // cannot be an online resource
$Saved_File = fopen($file, 'a+');
fwrite($Saved_File, $content);
fclose($Saved_File);
If this script is running on the same Server where you want to save the file, you can write a string into a file with "file_put_contents"(php.net).
Just think about the permissions you need to save a file, thats probably why it isn't working.
If you are trying to save it through HTTP, I think this is not possible.

Rewrite a file with PHP using HTML form

So I'm working on a little php file that is supposed to alter a specific file for the user. It gets the contents of the file and puts them into a textarea within a form. How can I make it so any edits done within this textarea will be rewritten to the file on the server? And even better, would I be able to allow the user to only edit certain lines, and have only those lines be rewritten?
Here's my code so far:
<?php
$filename = "../tree_c/index.php";
//$fp = fopen ($filename, "w"); <- doesn't seem to work for it opens empty file.
$contents = file_get_contents($filename);
/*
if (isset($_POST['field'])) {
// something here to rewrite the file.
*/
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<textarea name="field"><?php echo $contents ?></textarea>
<input type="submit" value="Save">
</form>
This should work fairly easily:
if (isset($_POST['field'])) {
file_put_contents($filename, $_POST['field']);
}
$datafile = "Files.txt";
$fp = fopen($datafile, "r");
$textdata= fgets($fp, 1024);
$text = '"'.$textdata.'"';
$this->set('text',$text);
if(!empty($this->data))
{
$datas = $this->data['data']['text']; //(your Textarea name)
$myFile = "Files.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $datas);
fclose($fh);
}
hope this ll help you....

PHP not writing client IP to file

I cant figure out why it wont write the client IP address to the file, everything else works.
<?php
$myFile = "ips.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $_SERVER['REMOTE_HOST']);
fclose($fh);
echo $_SERVER['REMOTE_ADDR'];
?>
Thanks in advance.
Maybe fwrite($fh, $_SERVER['REMOTE_ADDR']); ?
Why do you think that if echo $_SERVER['REMOTE_ADDR']; works,
fwrite($fh, $_SERVER['REMOTE_HOST']); should too?
Try echo $_SERVER['REMOTE_HOST']; first and see if it outputs something.
My guess: just change _HOST to _ADDR in fwrite as I said in the beginning.

Categories