mingger Posted May 11, 2011 Share Posted May 11, 2011 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 More sharing options...
fugix Posted May 11, 2011 Share Posted May 11, 2011 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 Link to comment https://forums.phpfreaks.com/topic/236122-creating-and-writing-a-file-by-form/#findComment-1213932 Share on other sites More sharing options...
mingger Posted May 11, 2011 Author Share Posted May 11, 2011 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 Link to comment https://forums.phpfreaks.com/topic/236122-creating-and-writing-a-file-by-form/#findComment-1213935 Share on other sites More sharing options...
fugix Posted May 11, 2011 Share Posted May 11, 2011 simple enough, are you having trouble with creating the file as well, or does it create the file but not write to it? Also, do you receive any errors when this script is run? Link to comment https://forums.phpfreaks.com/topic/236122-creating-and-writing-a-file-by-form/#findComment-1213948 Share on other sites More sharing options...
mingger Posted May 11, 2011 Author Share Posted May 11, 2011 It makes the file but it doesnt write anything into it. No it does not give any errors Link to comment https://forums.phpfreaks.com/topic/236122-creating-and-writing-a-file-by-form/#findComment-1213951 Share on other sites More sharing options...
fugix Posted May 11, 2011 Share Posted May 11, 2011 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); Link to comment https://forums.phpfreaks.com/topic/236122-creating-and-writing-a-file-by-form/#findComment-1213956 Share on other sites More sharing options...
mingger Posted May 11, 2011 Author Share Posted May 11, 2011 Okay now i know that it is not writable, but how to i make it writeable. Link to comment https://forums.phpfreaks.com/topic/236122-creating-and-writing-a-file-by-form/#findComment-1213960 Share on other sites More sharing options...
keepAway Posted May 11, 2011 Share Posted May 11, 2011 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); } } ?> Link to comment https://forums.phpfreaks.com/topic/236122-creating-and-writing-a-file-by-form/#findComment-1213963 Share on other sites More sharing options...
xyph Posted May 11, 2011 Share Posted May 11, 2011 CHMOD on unix servers. It has to do with permissions the user running PHP has on your server. Link to comment https://forums.phpfreaks.com/topic/236122-creating-and-writing-a-file-by-form/#findComment-1213966 Share on other sites More sharing options...
fugix Posted May 11, 2011 Share Posted May 11, 2011 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); Link to comment https://forums.phpfreaks.com/topic/236122-creating-and-writing-a-file-by-form/#findComment-1213969 Share on other sites More sharing options...
mingger Posted May 11, 2011 Author Share Posted May 11, 2011 it works for the most, but i want to make it as a php file so that it puts php code in it automatic. Link to comment https://forums.phpfreaks.com/topic/236122-creating-and-writing-a-file-by-form/#findComment-1213984 Share on other sites More sharing options...
fugix Posted May 11, 2011 Share Posted May 11, 2011 im not fully following, please explain what you would like to accomplish further. Are you able to write to the file now? Link to comment https://forums.phpfreaks.com/topic/236122-creating-and-writing-a-file-by-form/#findComment-1213987 Share on other sites More sharing options...
mingger Posted May 11, 2011 Author Share Posted May 11, 2011 I want to add menu tabs by the form Link to comment https://forums.phpfreaks.com/topic/236122-creating-and-writing-a-file-by-form/#findComment-1213989 Share on other sites More sharing options...
mingger Posted May 12, 2011 Author Share Posted May 12, 2011 Cant you help :/ Link to comment https://forums.phpfreaks.com/topic/236122-creating-and-writing-a-file-by-form/#findComment-1214573 Share on other sites More sharing options...
fugix Posted May 12, 2011 Share Posted May 12, 2011 insert the necessary code into your file Link to comment https://forums.phpfreaks.com/topic/236122-creating-and-writing-a-file-by-form/#findComment-1214575 Share on other sites More sharing options...
mingger Posted May 12, 2011 Author Share Posted May 12, 2011 But i dont know how and what i should do Link to comment https://forums.phpfreaks.com/topic/236122-creating-and-writing-a-file-by-form/#findComment-1214579 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.