dhuan Posted February 28, 2009 Share Posted February 28, 2009 how can i make the fputs/fwrite command writte at the beginning of the file instead of at the end of file? Here's my code: $fp=fopen($file,a); fwrite($fp,$save); I tested my code, it worked and all, the problem is, i want "$save" to be written before everything's been written, not after! (i'm sorry if i didnt make myself clear enough, english aint my first language, i'm tryin my best though!) Quote Link to comment https://forums.phpfreaks.com/topic/147356-question-about-fputs/ Share on other sites More sharing options...
dt192 Posted February 28, 2009 Share Posted February 28, 2009 from what i remeber you cant, just reverse the data when you read it back Quote Link to comment https://forums.phpfreaks.com/topic/147356-question-about-fputs/#findComment-773479 Share on other sites More sharing options...
dhuan Posted February 28, 2009 Author Share Posted February 28, 2009 you mind telling me how this can be done, please? Quote Link to comment https://forums.phpfreaks.com/topic/147356-question-about-fputs/#findComment-773481 Share on other sites More sharing options...
dt192 Posted February 28, 2009 Share Posted February 28, 2009 most read back functions read the lines into an array and i'm sure there is an array reverse function so you would just use that on the array or if not then just write a few lines of script to do it i'll have a look around and post you some code Quote Link to comment https://forums.phpfreaks.com/topic/147356-question-about-fputs/#findComment-773482 Share on other sites More sharing options...
dt192 Posted February 28, 2009 Share Posted February 28, 2009 $lines = split("\n", file_get_contents('filename')); array_reverse($lines); foreach ( $lines as $line ) {echo $line . '<br/>';} not tested but should be ok Quote Link to comment https://forums.phpfreaks.com/topic/147356-question-about-fputs/#findComment-773486 Share on other sites More sharing options...
dhuan Posted February 28, 2009 Author Share Posted February 28, 2009 HEY THANKS! i didnt do exactly what you told me, but you mentioned that code "file_get_contents" that i'd never heard of before. here's what i did: $old=file_get_contents($file); $save="$contentfilledintheform <br> $old"; $fp=fopen($file,w); fwrite($fp,$save); and guess what! it worked like a charm thanks again, pal! ;D ;D Quote Link to comment https://forums.phpfreaks.com/topic/147356-question-about-fputs/#findComment-773492 Share on other sites More sharing options...
dt192 Posted February 28, 2009 Share Posted February 28, 2009 yep yours would work better actually because it would only need to be reordered once when written rather than being reordered every time its read, good stuff :-D Quote Link to comment https://forums.phpfreaks.com/topic/147356-question-about-fputs/#findComment-773495 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.