Avendium Posted June 13, 2006 Share Posted June 13, 2006 How would go about taking a value from a form object and printing it on a line that I choose in an external file? for example. If i have the following in my external file, data.txt.data1data2data3data4and i want the user to input a value and that value be written on line 2 so that when the submit button is pressed the txt will now show. data1(whatever the input was)data3data4thanks Quote Link to comment https://forums.phpfreaks.com/topic/11822-solved-writing-with-php/ Share on other sites More sharing options...
poirot Posted June 13, 2006 Share Posted June 13, 2006 To retrieve the contents from the file use the file handling functions, being file_get_contents() the easiest in this case.Then, use str_replace() to replace data2 with the input. Something like:[code]$data = file_get_contents('data.txt');$data = str_replace('data2', $_POST['data'], $data);[/code][a href=\"http://www.php.net/file_get_contents\" target=\"_blank\"]http://www.php.net/file_get_contents[/a][a href=\"http://www.php.net/str_replace\" target=\"_blank\"]http://www.php.net/str_replace[/a] Quote Link to comment https://forums.phpfreaks.com/topic/11822-solved-writing-with-php/#findComment-44816 Share on other sites More sharing options...
Avendium Posted June 13, 2006 Author Share Posted June 13, 2006 hmm, this would work if i knew what "data2" was in order to replace it. The problem is, data 2 could be anything. Is there anyway to just target and replace the whole line without knowing what is there to begin with? and without replacing the first line? what im trying to do is this:I have my form in form.php. I want to send the data to my data.txt, on the line designated for that perticular form, in this case its line 2. So whenever the user inputs a new value, line 2 would be replaced with that new input. so i never know what line 2 holds to replace it. thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/11822-solved-writing-with-php/#findComment-44817 Share on other sites More sharing options...
AndyB Posted June 13, 2006 Share Posted June 13, 2006 Look at the file() function - [a href=\"http://ca.php.net/manual/en/function.file.php\" target=\"_blank\"]http://ca.php.net/manual/en/function.file.php[/a] - it reads a file into an array. Your 'data2' will be the second element of that array. You can then set the new value POSTed as equal to the second element and write the array back to the file. See the file write function for that.Remember that the first element of an array is $arr[0] not $arr[1].Create some code and we'll work with you from what you do. Quote Link to comment https://forums.phpfreaks.com/topic/11822-solved-writing-with-php/#findComment-44819 Share on other sites More sharing options...
Avendium Posted June 13, 2006 Author Share Posted June 13, 2006 I managed to load the file as an array and display the line that i want but im having trouble inputing the value that i get from my form back into the array.Form code[code]<form action="test.php" method="POST">name: <input type="text" name="name" /><input type="submit" value="Submit"/></form>[/code]PHP code[code]<? $filename = 'test.txt'; //filename if (file_exists($filename)) { $data = file($filename, 'r+'); //Initialize array //debug echo $data[0]; } else { echo "The file $filename does not exist"; break;}?>[/code]my txt file has the linesline0line1line2line3I can display them fine but im having trouble trying to figure out how to place the input back into the file to replace line1 which would be $data[1] in the array. What kind of write function should i use? Quote Link to comment https://forums.phpfreaks.com/topic/11822-solved-writing-with-php/#findComment-44840 Share on other sites More sharing options...
Barand Posted June 13, 2006 Share Posted June 13, 2006 try[code]if (isset($_POST['text'])) { $filename = 'test.txt'; //filename if (file_exists($filename)) { $data = file($filename); //Initialize array // replace second line $data[1] = $_POST['text'] . "\n"; // rewrite the file // ## remove '_' in filesys functions $fp = f_open($filename, 'w'); foreach ($data as $line) { f_write($fp, $line); } f_close($fp); } else { echo "The file $filename does not exist"; }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/11822-solved-writing-with-php/#findComment-44871 Share on other sites More sharing options...
Avendium Posted June 13, 2006 Author Share Posted June 13, 2006 Works great. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/11822-solved-writing-with-php/#findComment-45127 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.