9three Posted December 15, 2008 Share Posted December 15, 2008 Hello all I'm testing out the fopen function. <textarea id="elm4" name="index_info" rows="1" cols="1" style="width: 643px; height: 550px;"> <?php if(isset($_POST['save'])) { $myFile = "index_info.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "$index_info"; fwrite($fh, $stringData); fclose($fh); } include_once 'index_info.php'; ?> </textarea> <input type="submit" name="save" value="Save Settings" class="button"/> </form> What I'm trying to get done is view the current information in the file (ie. index_info.php), and if I decide to hit save it will update the file with whatever info I put in it. The problem is that it is deleting the info in the page and its not doing anything else. Quote Link to comment https://forums.phpfreaks.com/topic/137096-solved-fopen/ Share on other sites More sharing options...
genericnumber1 Posted December 15, 2008 Share Posted December 15, 2008 Did you set $index_info? If so, can you show that code? Quote Link to comment https://forums.phpfreaks.com/topic/137096-solved-fopen/#findComment-716063 Share on other sites More sharing options...
9three Posted December 15, 2008 Author Share Posted December 15, 2008 It's in the <textarea> name="index_info" Quote Link to comment https://forums.phpfreaks.com/topic/137096-solved-fopen/#findComment-716069 Share on other sites More sharing options...
premiso Posted December 15, 2008 Share Posted December 15, 2008 Your code is assuming that register_globals is on, this is a security flaw hence why the newer versions turn it off. Access index_info via $_POST['index_info'] instead. It's in the <textarea> name="index_info" Quote Link to comment https://forums.phpfreaks.com/topic/137096-solved-fopen/#findComment-716086 Share on other sites More sharing options...
9three Posted December 15, 2008 Author Share Posted December 15, 2008 hm that didnt do anything. it's still deleting everything inside. Quote Link to comment https://forums.phpfreaks.com/topic/137096-solved-fopen/#findComment-716093 Share on other sites More sharing options...
premiso Posted December 15, 2008 Share Posted December 15, 2008 Are you posting or getting the data. Try $_REQUEST. Also the reason it is deleteing everything is due to the "w" portion, try using the "a" for append. Read up at fopen the different types of access and which allows you to do what. Quote Link to comment https://forums.phpfreaks.com/topic/137096-solved-fopen/#findComment-716097 Share on other sites More sharing options...
9three Posted December 15, 2008 Author Share Posted December 15, 2008 I am using the POST method. I've read up on the fopen at php.net but its too vague and no real example is presented. Quote Link to comment https://forums.phpfreaks.com/topic/137096-solved-fopen/#findComment-716105 Share on other sites More sharing options...
premiso Posted December 15, 2008 Share Posted December 15, 2008 'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. Seems pretty straight forward to me. Use a+ if the file may not exist to open the file to write/append to it. Use a if you know the file exists and want to append to it, use just regular a. Using w, places the file pointer (where you start writing to the file) at the begining and truncates (deletes) the rest of the file so it is fresh. Quote Link to comment https://forums.phpfreaks.com/topic/137096-solved-fopen/#findComment-716109 Share on other sites More sharing options...
9three Posted December 15, 2008 Author Share Posted December 15, 2008 I've used a and a+ and it still deletes my information from my page. Quote Link to comment https://forums.phpfreaks.com/topic/137096-solved-fopen/#findComment-716114 Share on other sites More sharing options...
premiso Posted December 15, 2008 Share Posted December 15, 2008 Print out the entire post data to make sure that data is getting populated with print_r($_POST); Also post the full form if possible, that may give some insight to the problem. Quote Link to comment https://forums.phpfreaks.com/topic/137096-solved-fopen/#findComment-716118 Share on other sites More sharing options...
9three Posted December 15, 2008 Author Share Posted December 15, 2008 I thought I did, anyway: <form method="post" action="pages.php"> <textarea id="elm4" name="index_info" rows="1" cols="1" style="width: 643px; height: 550px;"> <?php if(isset($_POST['index_info'])) { $myFile = "../index_info.php"; $fh = fopen($myFile, 'a+') or die("can't open file"); $stringData = "$index_info"; fwrite($fh, $stringData); fclose($fh); } include_once '../index_info.php'; ?> </textarea> <input type="submit" name="save" value="Save Settings" class="button"/> </form> So I just gave it a 2nd try with "a+" it doesn't delete my information on my page but it doesn't update neither. Thats weird. Quote Link to comment https://forums.phpfreaks.com/topic/137096-solved-fopen/#findComment-716138 Share on other sites More sharing options...
9three Posted December 15, 2008 Author Share Posted December 15, 2008 Anyone got a clue? Quote Link to comment https://forums.phpfreaks.com/topic/137096-solved-fopen/#findComment-716154 Share on other sites More sharing options...
genericnumber1 Posted December 15, 2008 Share Posted December 15, 2008 <?php $myFile = "../index_info.php"; if(isset($_POST['index_info'])) { $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, $_POST['index_info']); fclose($fh); } ?> <form method="post" action="pages.php"> <textarea id="elm4" name="index_info" rows="1" cols="1" style="width: 643px; height: 550px;"><?php readfile($myFile); ?></textarea> <input type="submit" name="save" value="Save Settings" class="button"/> </form> should work, try that and if it doesn't, tell us. If it does, try to figure out how it differs from yours and what made the difference. (ps. you might want to look into file_put_contents() ) Quote Link to comment https://forums.phpfreaks.com/topic/137096-solved-fopen/#findComment-716199 Share on other sites More sharing options...
9three Posted December 16, 2008 Author Share Posted December 16, 2008 Yea that totally did it. I see a couple of things you did: 1. You put the location of my file outside the if statement 2. You used $_POST in the fwrite statement. 3. And you use readfile in the <textarea> Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/137096-solved-fopen/#findComment-716587 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.