php, write to file...
#1
Posted 10 July 2008 - 08:01 PM
#2
Posted 11 July 2008 - 05:57 PM
ref: http://php.net/manua...ction.fopen.php
Edited by rc69, 11 July 2008 - 05:57 PM.
#3
Posted 11 July 2008 - 10:11 PM
rc69, on Jul 11 2008, 05:57 PM, said:
ref: http://php.net/manua...ction.fopen.php
well, I tried that.... for some reason it wont work.
#4
Posted 11 July 2008 - 10:24 PM
Try rewind().
Otherwise, if we're backwards on file locations, mode 'a' for fopen() should do the trick. Post some of the code you have tried and the resulting files.
#5
Posted 13 July 2008 - 01:15 AM
rc69, on Jul 11 2008, 10:24 PM, said:
Try rewind().
Otherwise, if we're backwards on file locations, mode 'a' for fopen() should do the trick. Post some of the code you have tried and the resulting files.
Well I got it to write to to the beginning of the file...
When you submit content and use the a or a+ it always ends up writing to the end of the file I noticed.
When you use r+ it will clear all the content out and input ONLY what you typed in.
such as:
in data.txt:
test
test
test
using r+ will do this to data.txt:
test hahaa
So my question now is, how do i write to the beginning of the file using r+ and making new lines? this is what i use
$data = $_POST['data'];
$data = "$data\n";
$data = nl2br($data);
#6
Posted 13 July 2008 - 12:17 PM
bigdfbc2008, on Jul 12 2008, 11:15 PM, said:
...
So my question now is, how do i write to the beginning of the file using r+ and making new lines? this is what i use
You posted some code for how you get the data, could you post the code from fopen() to fclose()?
Also, for getting the data:
$data = nl2br($_POST['data']).'<br />';
Edited by rc69, 13 July 2008 - 12:17 PM.
#7
Posted 14 July 2008 - 12:23 AM
rc69, on Jul 13 2008, 01:17 PM, said:
bigdfbc2008, on Jul 12 2008, 11:15 PM, said:
...
So my question now is, how do i write to the beginning of the file using r+ and making new lines? this is what i use
You posted some code for how you get the data, could you post the code from fopen() to fclose()?
Also, for getting the data:
$data = nl2br($_POST['data']).'<br />';
What i ment was when you write to the file using r+ it doesn't write multiple lines. Say I submit "hey how are you?" and then i submit "I am great"... Well, before submiting "I am great", "hey how are you?" is already in the data file, but when you send the "I am great" to the data file using r+ it clears out "hey how are you?" instead of adding "I am great" above "hey how are you" get what I mean?
#8
Posted 14 July 2008 - 12:48 PM
$data = file_get_contents($filename).nl2br($_POST['data']).'<br />'; $fp = fopen($filename, 'w'); fwrite($fp, $data); fclose($fp);Let me know if you need an explaination of this or if it even works (which, typo's aside, it should)
Edited by rc69, 14 July 2008 - 12:49 PM.
#9
Posted 15 July 2008 - 09:37 PM
rc69, on Jul 14 2008, 01:48 PM, said:
$data = file_get_contents($filename).nl2br($_POST['data']).'<br />'; $fp = fopen($filename, 'w'); fwrite($fp, $data); fclose($fp);Let me know if you need an explaination of this or if it even works (which, typo's aside, it should)
<?php
$file = "demo1.txt";
if (! file_exists( $file ) )
{
$fp = fopen( $file, 'w' );
fclose($fp);
echo('File Created. <a href="test.php">Return</a>');
}
else
{
if ( file_exists( $file ) && is_file( $file ) )
{
if ( isset( $_POST['add'] ) )
{
$data = file_get_contents( $file ) . nl2br( $_POST['data'] ) . "<br \>\n";
$fw = fopen( $file, 'r+' );
fseek( $fw, 0, SEEK_CUR );
fwrite( $fw, $data );
fclose( $fw );
echo('Wrote to file.<br />'.$data.'<br><a href="test.php">Return</a>');
}
else
{
$fp = fopen( $file, 'r+' );
$get = fread( $fp, 2094 );
fclose( $fp );
echo $get;
echo('<hr>');
echo('<form action="'.$_SERVER['PHP_SELF'].'" method="post">');
echo('<input type="text" name="data">');
echo('<input name="add" type="submit">');
echo('</form>');
}
}
}
?>
well it works, but it doesn't write to the beginning of the file... hmmm
#10
Posted 15 July 2008 - 10:54 PM
$fp = fopen($filename, 'w'); fwrite($fp, $data); fclose($fp);
php.net said:
As a side note:
if ( file_exists( $file ) && is_file( $file ) )Your file either exists or it doesn't. About five lines up you preform that check. Due to the nature of an if-else statement, there is no need to check again at the above line.
Edited by rc69, 15 July 2008 - 10:58 PM.
#11
Posted 16 July 2008 - 12:04 AM
rc69, on Jul 15 2008, 11:54 PM, said:
$fp = fopen($filename, 'w'); fwrite($fp, $data); fclose($fp);
php.net said:
As a side note:
if ( file_exists( $file ) && is_file( $file ) )Your file either exists or it doesn't. About five lines up you preform that check. Due to the nature of an if-else statement, there is no need to check again at the above line.
ya I know but I did it anyways lol I planed on removing it sooner or later but I really just want this to work first...
#12
Posted 28 July 2008 - 04:32 AM
$fp = fopen($filename, 'w');
fwrite($fp, $data);
fclose($fp);
Edited by sahal, 28 July 2008 - 05:07 AM.
#13
Posted 28 July 2008 - 02:10 PM
Not sure if you've resolved this yet or not. So you want to add to a file but add to the top of the file? I will propose a way to do this... not the cleanest per se, but it may suit your need. First we open the file, save the contents into a var and close the file. Then we open the file for writing and we write the new contents and then write the old contents afterwards... tada! Maybe not that exciting...
<?php
$myFile = "fileToWriteTo.txt";
// First we will read the file and save the contents to a variable
$fh = fopen($myFile, 'r');
// Save the contents into $data
$data = fread($fh, filesize($myFile));
fclose($fh);
// Now we open the file for writing, all contents are cleared from the file
$fh = fopen($myFile, 'w') or die("can't open file");
$newContent = "Newest content at the top.";
// Now we are writing the new contents to the empty file
fwrite($fh, $newContent);
// We add a new line before the old contents and then add it to the bottom of the file
$data = "\n" . $data;
fwrite($fh, $data);
fclose($fh);
?>
This should do the trick if all you wanted was to add newer content to the top of the file.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
