I need this script to write the data to the beginning of the file, rather than the end. SCRIPT: <html> <head> <title>Posting</title> </head> <body> <?php $date = $_GET["date"]; $title = $_GET["title"]; $message = $_GET["message"]; print("Post submitted."); $out = fopen($_SERVER['DOCUMENT_ROOT'].'/blogfile.txt', "a"); if (!$out) { print("Could not append to file"); exit; } fputs ($out,implode,("\n")); fwrite($out,"$date|$title|$message"); fwrite($out,("\n")); fclose($out); ?> </body> </html> PHP: I know it may not seem logical to you, but it does to me. Thanks for the help.
Change this: $out = fopen($_SERVER['DOCUMENT_ROOT'].'/blogfile.txt', "a"); PHP: to this: $out = fopen($_SERVER['DOCUMENT_ROOT'].'/blogfile.txt', "r+"); PHP: Here is the applicable section in the manual for the PHP fopen function.
<html> <head> <title>Posting</title> </head> <body> <?php $date = $_GET["date"]; $title = $_GET["title"]; $message = $_GET["message"]; print("Post submitted."); $file = $_SERVER['DOCUMENT_ROOT'].'/blogfile.txt'; $out = fopen($file, "r+"); if (!$out) { print("Could not append to file"); exit; } $old_content = fread($out, filesize($file)); fputs ($out,implode,("\n")); fwrite($out,"$date|$title|$message"); fwrite($out,("\n")); fwrite($out, $old_content); fclose($out); ?> </body> </html> PHP:
Have you tried opening it for appending mode (like the original code) but then using something like: fseek($out, 0); PHP: Which should rewind the pointer to the beginning?
<html> <head> <title>Posting</title> </head> <body> <?php $date = $_GET["date"]; $title = $_GET["title"]; $message = $_GET["message"]; print("Post submitted."); $file = $_SERVER['DOCUMENT_ROOT'].'/blogfile.txt'; $file2 = $_SERVER['DOCUMENT_ROOT'].'/blogfile2.txt'; $old = fopen($file, "r"); $old_content = fread($old, filesize($file)); $new_content = fread($new, filesize($file2)); $new = fopen($file2, "w+"); fwrite($new, $old_content); fclose($new); $old = fopen($file, "w"); fclose($old); $old = fopen($file, "a"); fputs ($old,implode,("\n")); fwrite($old,"$date|$title|$message"); fwrite($old,("\n")); fwrite($old, $new_content); fclose($old); ?> </body> </html> PHP: I'm trying to use a new file as a sort of "cache," but it's not working. Any ideas why?
I had a similar problem that I solved by using the append (putting the new info in at the end of the file) and then displaying it by reading it back via reversing the array. Array_Reverse function info here: http://uk.php.net/function.array-reverse I've changed my code recently but I think it looked like this (reading the file back): HTH.
GOT IT!!!! <html> <head> <title>Posting</title> </head> <body> <?php $date = $_GET["date"]; $title = $_GET["title"]; $message = $_GET["message"]; print("Post submitted."); $file = $_SERVER['DOCUMENT_ROOT'].'/blogfile.txt'; $file2 = $_SERVER['DOCUMENT_ROOT'].'/blogfile2.txt'; { //Imports old data $old = fopen($file, "r"); $old_content = fread($old, filesize ($file)); fclose($old); //Writes new data $new = fopen($file2, "w"); fputs ($new,implode,("\n")); fwrite($new,"$date|$title|$message"); fwrite($new,("\n")); fwrite($new,$old_content); fclose($new); //Imports new data $new = fopen($file2, "r"); $new_content = fread($new, filesize ($file2)); fclose($new); //Replaces old data $old = fopen($file, "w+"); fwrite($old,$new_content); fclose($old); } ?> </body> </html> PHP:
If you do the whole write in one. And then close the file; why not: // With file_get_contents() $file = 'log.txt'; $data = date("H:i:s",time())."\tThis is some \n text; foo was here.\n"; $log = fopen($file,is_file($file)?"r+":"w+"); fwrite($log,$data.file_get_contents($file)); fclose($log); PHP: ;or... //With file() $file = "log.txt"; $data = date("H:i:s",time())."\tThis is some \n text; foo was here.\n"; $log = fopen($file,is_file($file)?"r+":"w+"); fwrite($log,$data.implode('',file($file))); fclose($log); PHP: ** And from your code in first post; <html> <head> <title>Posting</title> </head> <body> <?php $date = $_GET["date"]; $title = $_GET["title"]; $message = $_GET["message"]; $bfile = $_SERVER['DOCUMENT_ROOT'].'/blogfile.txt'; print("Post submitted."); if(!$out=fopen($bfile,is_file($bfile)?"r+":"w+")) die("Could not append to file"); fwrite($out,"$date|$title|$message\n".file_get_contents($bfile)); fclose($out); ?> </body> </html> PHP: