Jump to content

Archived

This topic is now archived and is closed to further replies.

AdRock

Writing to a text file instead of database [RESOLVED]

Recommended Posts

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 document

Here 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.php

Has anyone got any ideas how I could do this?

Share this post


Link to post
Share on other sites
something like this:

[code]<?php
$name = $_POST['name']; // vars from form
$filename = 'data.txt'; // textfile name

if (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]

Share this post


Link to post
Share on other sites
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]

Regards
Huggie

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
To delete the contents of a text file try:

[code]
<?php

if ( 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]
<?php

if ( isset ( $_POST['submit'] ) ) {

$file = "whatever.txt";

$fp = fopen( $file, "a" ); // added append which is what "a" means

if ( 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.

Share this post


Link to post
Share on other sites

×

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.