Jump to content


php, write to file...


12 replies to this topic

#1 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 10 July 2008 - 08:01 PM

What I want to do is when I submit content to the data file, instead of the data submited being at the end of the file, i want it to be at the beginning. so all new content will be at the top. I know how to do it in mysql so don't bother submiting any replies with mysql, I want to learn how to do it in basic file system codes... would I use the feof function?

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 11 July 2008 - 05:57 PM

Assuming you're using fopen() and fwrite(), you want to use mode 'x' when you call fopen().

ref: http://php.net/manua...ction.fopen.php

Edited by rc69, 11 July 2008 - 05:57 PM.


#3 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 11 July 2008 - 10:11 PM

View Postrc69, on Jul 11 2008, 05:57 PM, said:

Assuming you're using fopen() and fwrite(), you want to use mode 'x' when you call fopen().

ref: http://php.net/manua...ction.fopen.php


well, I tried that.... for some reason it wont work.

#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 11 July 2008 - 10:24 PM

You do want to write to the begining of the file right (i.e. the top)? If fopen('foo', 'x'); didn't work, feof() won't either (it only checks to see if you are at the end of the file, which isn't where you want to be).

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 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 13 July 2008 - 01:15 AM

View Postrc69, on Jul 11 2008, 10:24 PM, said:

You do want to write to the begining of the file right (i.e. the top)? If fopen('foo', 'x'); didn't work, feof() won't either (it only checks to see if you are at the end of the file, which isn't where you want to be).

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 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 13 July 2008 - 12:17 PM

View Postbigdfbc2008, on Jul 12 2008, 11:15 PM, said:

Well I got it to write to to the beginning of the file
...
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
Umm, wait, what? You got it to write to the beginning of the file, but you don't know how to get it to write to the beginning of the file?

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 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 14 July 2008 - 12:23 AM

View Postrc69, on Jul 13 2008, 01:17 PM, said:

View Postbigdfbc2008, on Jul 12 2008, 11:15 PM, said:

Well I got it to write to to the beginning of the file
...
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
Umm, wait, what? You got it to write to the beginning of the file, but you don't know how to get it to write to the beginning of the file?

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 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 14 July 2008 - 12:48 PM

Well, i'll admit that is weird. Unfortunately, as i can't think of an easier alternative, there is always brute force:
$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) :popcorn:

Edited by rc69, 14 July 2008 - 12:49 PM.


#9 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 15 July 2008 - 09:37 PM

View Postrc69, on Jul 14 2008, 01:48 PM, said:

Well, i'll admit that is weird. Unfortunately, as i can't think of an easier alternative, there is always brute force:
$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 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 15 July 2008 - 10:54 PM

Please, read my code carefully.
$fp = fopen($filename, 'w');
fwrite($fp, $data);
fclose($fp);

php.net said:

'w' = Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length

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 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 16 July 2008 - 12:04 AM

View Postrc69, on Jul 15 2008, 11:54 PM, said:

Please, read my code carefully.
$fp = fopen($filename, 'w');
fwrite($fp, $data);
fclose($fp);

php.net said:

'w' = Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length

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 sahal

    Young Padawan

  • Members
  • Pip
  • 2 posts

Posted 28 July 2008 - 04:32 AM

try this coding to write in open file:-

$fp = fopen($filename, 'w');
fwrite($fp, $data);
fclose($fp);

Edited by sahal, 28 July 2008 - 05:07 AM.


#13 Tyson D

    Young Padawan

  • Members
  • Pip
  • 85 posts
  • Gender:Male
  • Location:Canada

Posted 28 July 2008 - 02:10 PM

Hey BIGDFBC,

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