1

So I am trying to open a manifest File, add a A row of text to last row of text (ex.#Revision Date 21-12-2016), as of now what happens is i get this #Revision Date 20-12-2016#Revision Date 21-12-2016 all on the same line. instead i would like this after each time i run the script, to add the the new line under the old line

#Revision Date 20-12-2016
#Revision Date 21-12-2016
#Revision Date 22-12-2016

I have tried many different solutions like . PHP_EOL, "\n" here is my script that i have been using

Script

<?php
$filename2 = 'newapp/cache.manifest';
$current1 = file_get_contents($filename2);
$current1 .= "John Smith". PHP_EOL;
file_put_contents($filename2, $current1);
?>
user3015877
  • 33
  • 1
  • 7
  • 1
    Your code worked fine for me. What OS are you running this under? – Funk Forty Niner Dec 21 '16 at 18:35
  • `$line = "john";` and `file_put_contents($filename2, $line . PHP_EOL, FILE_APPEND);` there is no need to `file_get_contents()` first. – Xorifelse Dec 21 '16 at 18:36
  • 1
    @Xorifelse Even without `FILE_APPEND`, I tested their code and it added it under a new line. I deleted a comment about using that, maybe I shouldn't have. – Funk Forty Niner Dec 21 '16 at 18:36
  • @Fred-ii- I guess its just one of those days: "Why isn't my code working!!", while it actually works.. – Xorifelse Dec 21 '16 at 18:41
  • sorry forgot to add chrome windows – user3015877 Dec 21 '16 at 18:48
  • 1
    *"sorry forgot to add chrome windows"* - what do you mean by that? That you're running this under Windows? and using which, xampp, wamp, other? `PHP_EOL` is cross-platform so that should work. Also how are you using this, as `http://localhost` or straight in your browser as `file:///`? – Funk Forty Niner Dec 21 '16 at 18:50
  • 1
    if you're under windows, then use `"\r\n"` - the `\n` is for Linux only. But I still can't see why `PHP_EOL` would fail you here. – Funk Forty Niner Dec 21 '16 at 18:52
  • Ok going to re-edit question as i figured it out what i have done wrong, i didn't realize that txt file would make a difference when i was actually editing a manifest file. Just figured it was the same and less complicated, so sorry for the confusion, i am going to re-edit question. – user3015877 Dec 21 '16 at 18:57
  • @Fred-ii- The only plausible reason is that he echo's the file content to the browser (not checking the source noor the file itself in notepad) – Xorifelse Dec 21 '16 at 18:58
  • 1
    @Xorifelse I've added my answer below which I feel is what's going on. – Funk Forty Niner Dec 21 '16 at 18:58
  • i am not echo file out into screen, i am looking at the file directly. – user3015877 Dec 21 '16 at 19:02
  • 1
    since you're using this in a manifest as you said in comments, we need to know how you're using this. Your question IMHO is still unclear. – Funk Forty Niner Dec 21 '16 at 19:05
  • 1
    @user3015877 did you not try `"\r\n"` instead of `PHP_EOL`? I did write that out in an answer below also. – Funk Forty Niner Dec 21 '16 at 19:08
  • sorry was trying out, and yes that did work `$current1 .= 'John Smith'. "\r\n";` thanks so much for yours and everyone's else help, and sorry about the confusion, much appreciated – user3015877 Dec 21 '16 at 19:16
  • 1
    @user3015877 I'm glad to hear that the `\r\n` worked for you. I find it rather odd though that the `PHP_EOL` did not work for you, *most bizarre*. You're welcome, *cheers* – Funk Forty Niner Dec 21 '16 at 19:20
  • 1
    @user3015877 I'm curious; which PHP version are you running this under? the exact version - `PHP_EOL` was introduced in PHP 5.0.2 http://php.net/manual/en/reserved.constants.php - if it's lower, than that may very well be why it didn't work. – Funk Forty Niner Dec 21 '16 at 19:24
  • This hosting package is currently running PHP Version: 5.6, just to add the original script I post does work properly if it is a txt file. I was actually updating manifest file and it would not work. But using `\r\n` does work for manifest file. – user3015877 Dec 21 '16 at 19:42
  • 1
    @user3015877 Oh, I see. Therefore the .`manifest` file extension may be the reason because of its extension, and could be a host/server-specific issue. – Funk Forty Niner Dec 21 '16 at 19:43

2 Answers2

2

Since you're running this under a Windows environment, use "\r\n" instead of "\n", since \n is for *NIX systems.

However, I can't see why PHP_EOL wouldn't work in this instance, since that is cross-platform.

If you're trying to echo this on screen, then that's the reason why it's showing you that in one line.

\n or \r\n will appear on a new line in the file, as opposed to on screen which is a completely different animal altogether.

So echo on a different line for each, you'd need to use <br> or <p> tags, or as mentioned in a comment by Xorifelse nl2br(file_get_contents($filename2));

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

Use the FILE_APPEND flag to add to an existing file (also LOCK_EX if you want to ensure nobody else edits the file at the same time).

$filename2 = 'newapp/cache.txt';
$new_text = "John Smith\n";
file_put_contents($filename2, $new_text, FILE_APPEND | LOCK_EX);
jackel414
  • 1,636
  • 2
  • 13
  • 29
  • 1
    sure but that doesn't explain why their code is failing them, where as I tested it on Xampp/Windows which worked fine using `PHP_EOL` which is cross-platform under all OS's – Funk Forty Niner Dec 21 '16 at 18:41
  • Well, based on the code that's in the question, the single line result makes sense. They're adding the "John Smith" to the end of their existing file without adding a line break first. – jackel414 Dec 21 '16 at 18:42
  • 1
    Even if there isn't a line break at the end of the last entry, it should still create a new one for each new additions; I tested this theory already. – Funk Forty Niner Dec 21 '16 at 18:43
  • The result of file_get_contents will just be a string. Correct me if I'm wrong, but if there's no explicit line break at the end of that string, then concatenating another string will not create one. – jackel414 Dec 21 '16 at 18:47
  • I suggest you try their code on your own machine, if you have php etc. installed on your pc. You'll see that what they posted and even under a \*NIX system, it should still work. – Funk Forty Niner Dec 21 '16 at 18:51
  • ok i tried the above script and this is what i get `John Smith John Smith John Smith John Smith John Smith ` – user3015877 Dec 21 '16 at 18:51
  • I am not if this matters as i am going to try now is that the txt file is a manifest file, did not think it matter, but am going to try now – user3015877 Dec 21 '16 at 18:52
  • @user3015877 Are you seriously echoing the data with `echo file_get_contents($filename2)` to see the results in your browser? Check the `txt` file instead or in your browser press `CRTL + U` or `nl2br(file_get_contents($filename2));` – Xorifelse Dec 21 '16 at 18:55
  • user3015877 - make sure you have the line breaks in there. fred-ii - i just tested this on my macOS and it works as I described. If the original file has no line break at the end, the resulting text will all be on one line. – jackel414 Dec 21 '16 at 18:57
  • @jackel414 Different OS's will react differently using different end of line syntax. I feel that the question goes deeper than we originally thought. Let's only hope we can find the true reason here ;-) – Funk Forty Niner Dec 21 '16 at 19:00
  • @Fred-ii- Yeah, I agree. Odd that the PHP_EOL wouldn't work (as you mentioned in your answer). I would expect that to be OS agnostic. – jackel414 Dec 21 '16 at 19:03
  • @jackel414 PHP_EOL didn't work on macOS? that is most bizarre. Found this Q&A http://stackoverflow.com/q/128560/1415724 – Funk Forty Niner Dec 21 '16 at 19:05
  • 1
    @Fred-ii- sorry - I meant that it seemed to not work for OP. It works for me. – jackel414 Dec 21 '16 at 19:08
  • @jackel414 ok. Well... at this point in time, I don't know where to throw myself *lol*. I asked the OP if using `"\r\n"` worked for them or not, but haven't responded anywhere. I did post an answer of my own (no competition on yours here) ;-) so I'll just leave it for now. I can't spend anymore time on something I don't know what I'm dealing with. – Funk Forty Niner Dec 21 '16 at 19:10
  • @jackel414 Update: seems the `\r\n` did the trick for them. Why the `PHP_EOL` did not, is the big question. – Funk Forty Niner Dec 21 '16 at 19:18