Jump to content

[SOLVED] Putting text into a file


johng

Recommended Posts

This code is supposed to create a file and insert some text into it.  I had it working, but the file was not being put in the right place, so when I went to change it, this is what I got, and it doesn't put it there. 

 

<?php
if (isset($_POST['submit'])) {
    $ho = (file_exists('\files\field1.txt'))?'a':'w'; // open with append if file exists
    $fp=fopen('\files\field1.txt',$ho);
    fwrite($fp, $_POST['field2']);
    fclose($fp);
}
?>
<html>
<head>
<title>Test Form</title>
</head>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="field1" size="20"><br>
<input type="text" name="field2" size="20"><br>
<input type="submit" name="submit" value="Send Information">
</form>
</body>
</html>

 

 

Any suggestions?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/44012-solved-putting-text-into-a-file/
Share on other sites

 

<form method="POST">
<table border="1" align="center" width="300">
  <tr>
    <td colspan="2" align="center">How to Change the World?</td>
  </tr>
  <tr>
    <td align="center"><img src="../images/atlas.jpg" width="100" height="165" /></td>
    <td><textarea name = "content" cols="50" rows="10"></textarea></td>
  </tr>
  <tr>
    <td colspan="2">
<?
if(isset($_POST['content'])) { //Page was submitted
    if (!$_POST['content']) { //value of content is empty
        $output = "There was no data entered!";
	echo $output;

    } else { //Content has value
        $content = $_POST['content'];
        $info = $_POST['info'];
         
        @$fp = fopen("name of your text file", "a"); //enter the name of your file here to open it
  
        fwrite($fp, $content."\r\n");
        
        $info = file('name of your text file  '); //enter the name of the file here to read from it
        for ($i = 0; $i < count($info); $i++) {
           echo '<br><hr>';
           echo nl2br(htmlentities(stripslashes(trim($info[$i])),ENT_QUOTES));
        }
        fclose($fp);
    }
}
?>

Thanks for the help!  I got it working, I was just using the wrong way of slashes ('\' instead of '/')

 

Thanks again!

 

Here's the code, if anyone else wants it.  (I have also modified it so that the name of the file saves as the name of the first input field.  This is also something that I couldn't get it to do with the wrong slash.)

 

<?php
if (isset($_POST['submit'])) {
    $ho = (file_exists("files/$field1.txt"))?'a':'w'; // open with append if file exists
    $fp=fopen("files/$field1.txt",$ho);
    fwrite($fp, $_POST['field2']);
    fclose($fp);
}
?>
<html>
<head>
<title>Test Form</title>
</head>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="field1" size="20"><br>
<input type="text" name="field2" size="20"><br>
<input type="submit" name="submit" value="Send Information">
</form>
</body>
</html>

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.