I have some problems with saving some strings into a .txt
file.
I have a textarea
on my client side form, which is used with summernote and I sent the form with POST
to my server, which got it, doesn't matter, how long is the text, I know, because I print it. But, at the moment when I want to save it to a .txt
file then I only can save 369-370 characters! If there is more then the PHP
ignore it, and doesn't save anything. This is how I save it:
fwrite($file, iconv('utf-8','ISO-8859-2',$text."\r\n"));
UPDATE I open the file with:
$file = fopen("file.txt","a") or die ('<h1 class="dangermsg"> <i class="fa fa-exclamation-triangle"></i>Error msg!</h1>');
How can I save more characters, or how can I resolve this problem?
When dealing with text encoding it's usually safer to open the file in binary mode by adding b
to your mode, so in this case use ab
instead of a
.
Per the docs: https://www.php.net/manual/en/function.fopen.php
If you do not specify the 'b' flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with \r\n characters.
For portability, it is strongly recommended that you always use the 'b' flag when opening files with fopen().
It is also strongly recommended that you re-write code that uses or relies upon the 't' mode so that it uses the correct line endings and 'b' mode instead.
Also use PHP_EOL
constant instead of \r\n
, this allows for cross-OS functionality.