AdRock Posted October 11, 2006 Share Posted October 11, 2006 I have a variable that I need to change on a regular basis and it needs to be done using a form.I don't want to create a table in a database just for this one variable so would like to know how i can write it to a text documentHere is the code in index.php that creates an thermometer image. The bit that needs to be changed is the [u]current[/u]. [code]<img src="/includes/thermometer/thermo.php?current=10000&max=15000" hspace="55">[/code]I thought saving the current variable to a text file would work unless there is a way of updating the variable within index.phpHas anyone got any ideas how I could do this? Quote Link to comment https://forums.phpfreaks.com/topic/23654-writing-to-a-text-file-instead-of-database-resolved/ Share on other sites More sharing options...
Gruzin Posted October 11, 2006 Share Posted October 11, 2006 something like this:[code]<?php$name = $_POST['name']; // vars from form$filename = 'data.txt'; // textfile nameif (is_writable($filename)) {if (!$handle = fopen($filename, 'a')) {echo "Cannot open file ($filename)";exit;}if (fwrite($handle, $name) === FALSE) {echo "Cannot write to file ($filename)";exit;}header ('Location: thanks.php');?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/23654-writing-to-a-text-file-instead-of-database-resolved/#findComment-107379 Share on other sites More sharing options...
HuggieBear Posted October 11, 2006 Share Posted October 11, 2006 It's very easy, look at these functions:For reading: [url=http://uk.php.net/manual/en/function.file-get-contents.php]file_get_contents()[/url]For Writing: [url=http://uk.php.net/manual/en/function.fopen.php]fopen()[/url], [url=http://uk.php.net/manual/en/function.fwrite.php]fwrite()[/url], [url=http://uk.php.net/manual/en/function.fclose.php]fclose()[/url]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23654-writing-to-a-text-file-instead-of-database-resolved/#findComment-107380 Share on other sites More sharing options...
AdRock Posted October 14, 2006 Author Share Posted October 14, 2006 Is there a way of deleting what is in the current text file or overwriting it?I have got it to write to the text file but how do I update what's in there or can't it be done? Quote Link to comment https://forums.phpfreaks.com/topic/23654-writing-to-a-text-file-instead-of-database-resolved/#findComment-108778 Share on other sites More sharing options...
HuggieBear Posted October 14, 2006 Share Posted October 14, 2006 Yes, look at the second argument that fopen() accepts.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23654-writing-to-a-text-file-instead-of-database-resolved/#findComment-108848 Share on other sites More sharing options...
dan245 Posted October 14, 2006 Share Posted October 14, 2006 To delete the contents of a text file try:[code]<?phpif ( isset ( $_POST['submit'] ) ) {$file = "whatever.txt";$fp = fopen( $file, "w" );if ( fwrite ( $fp, "" ) ) { print "The file $file has been erased!"; } else { print "Writing to the file: $file has failed!"; }}?>[/code]If you would like to overwrite the file, put text in the empty quotes in this line:[code]if ( fwrite ( $fp, "This will overwrite the text!" ) ) {[/code]Then replace that line with the other line of that code.As for updating text try doing this:[code]<?phpif ( isset ( $_POST['submit'] ) ) {$file = "whatever.txt";$fp = fopen( $file, "a" ); // added append which is what "a" meansif ( fwrite ( $fp, "text1<br>text2<br>text3<br>text4<br>text5" ) ) { print "The file $file has been appended too!"; } else { print "Writing to the file: $file has failed!"; }}?>[/code]-Dan M. Quote Link to comment https://forums.phpfreaks.com/topic/23654-writing-to-a-text-file-instead-of-database-resolved/#findComment-108856 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.