Jump to content

Creating and writing a file by form


mingger

Recommended Posts

Hey guys

 

i've been trying to find out how to create and and write in a file, so that you can for example add a nav button to your website by form (you know like a admin panel). It tried some things but it doesnt work cause it wont write to the file... here is the code i've used:

 

$ourFileName = $_POST['sitename'];

$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");

fclose($ourFileHandle);

 

$myFile = $_POST['sitename'];

$fh = fopen($myFile, 'w') or die("can't open file");

$stringData = "test\n";

fwrite($fh, $stringData);

$stringData = "test\n";

fwrite($fh, $stringData);

fclose($fh);

 

the "sitename" is the id from the form where you choose what the menu tab should be called.

I would really glad if you could help me out with this one thanks!

 

MinG

Link to comment
https://forums.phpfreaks.com/topic/236122-creating-and-writing-a-file-by-form/
Share on other sites

the first part of this code is not necessary..can shorten it to this

$myFile = $_POST['sitename'];
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "test\n";
fwrite($fh, $stringData);
$stringData = "test\n";
fwrite($fh, $stringData);
fclose($fh);

also, can you show the form that you use to do this please and any php that pertains

This is the form, and there is no more php code:

 

<form action="myform.php" method="post">

<p>Site name <input type="text" name="sitename" />remember to put .php<br />

title <input type="text" name="title" /></p>

 

<p>Do you like this website?

<br><input type="radio" name="likeit" value="Yes" checked="checked" /> Yes

<br><input type="radio" name="likeit" value="No" /> No

<br><input type="radio" name="likeit" value="Not sure" /> Not sure</p>

 

<p>Your comments:<br />

<textarea name="comments" rows="10" cols="40"></textarea></p>

 

<p><input type="submit" value="Send it!"></p>

</form>

 

btw. thanks for helping

okay we are going to add some simple code to check and make sure that the file is writable...try

$myFile = $_POST['sitename'];
$fh = fopen($myFile, 'w') or die("can't open file");
if(is_writable($myfile))  {

   echo "this file is writable \n";
} else  {

  echo "this file is not writable \n";
}
$stringData = "test\n";
fwrite($fh, $stringData);
$stringData = "test\n";
fwrite($fh, $stringData);
fclose($fh);

Check this out.

<form action="" method="post">
<table cellspacing=2 cellpadding=2 border=0 align=center>
<tr><td><strong>Site name: </strong></td><td><input type="text" name="sitename" /></td><td>remember to put .php</td></tr>
<tr><td><strong>Title: </strong></td><td colspan=2><input type="text" name="title" /></td></tr>
<tr><td colspan=3><strong>Do you like this website? </strong></td></tr>
<tr><td colspan=3><input type="radio" name="likeit" value="Yes" checked="checked" /> Yes</td></tr>
<tr><td colspan=3><input type="radio" name="likeit" value="No" /> No</td></tr>
<tr><td colspan=3><input type="radio" name="likeit" value="Not sure" /> Not sure</td></tr>
<tr><td><strong>Your comments: </strong></td></tr>
<tr><td colspan=3><textarea name="comments" rows="10" cols="40"></textarea></td></tr>
<tr><td colspan=3 align=center><input type="submit" name="submit" value="Send it!"></td></tr>
<tr><td align=center colspan=3><?php if (isset($_POST['submit'])) echo "Your replay has been added.";?></td></tr>
</form>


<?php
if (isset($_POST['submit']))
  {
  
  $comment = "Site name: ".$_POST['sitename']."
Title: ".$_POST['title']."
Do you like the website: ".$_POST['likeit']."
Your comments: ".$_POST['comments'];
  
  $file = "replays.txt";		   
		   
  $border = "\r\n----------------------------------------\r\n";		 

      if (!file_exists($file))
  file_put_contents($file, $comment);
  else
  {
  $handle = fopen($file, "a+");
  fwrite($handle, $border.$comment);
  fclose($handle);
  }	  
  } 


?>

sounds like a permissions problem, try this

$myFile = $_POST['sitename'];
$fh = fopen($myFile, 'w') or die("can't open file");
$chmod = chmod($fh, 0777);
$stringData = "test\n";
fwrite($fh, $stringData);
$stringData = "test\n";
fwrite($fh, $stringData);
fclose($fh);

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.