PHP: creating a file with the fopen () function results in a "cryptic" value on the line, repeated with existing files

I am trying to save a long string (> 700,000 characters) to a file on the server using the fopen () function.

$file = fopen($filename, 'w+');
fwrite($file, $content);
fclose($file);
echo $content;

      

But anyway, this only leads to the correct contents of the file when the file already exists. If the file doesn't exist, it is created with "cryptic" content, which doesn't make any sense to me. (While echoing $ content displays correct values)

An example of the first few characters of a file:

When I reload the script, the file already exists and the content is written correctly and echoed correctly as before.

This also works when I create the file myself via FTP (no matter what content it had before, I tried "Hello" too). As long as the file already exists, everything is fine.

It is also very strange that if I insert var_dump to see that the $ content value is at a certain point, the file is written with the correct content, but the echo returns a "cryptic" string. It seems that var_dump always has the correct values.

I have already tried using almost all the different fopen () mods like "wb", "wb +", "wt", "wt +", "xt", etc. I checked paths, permissions, and searched google all night long. No success.

This worries me as I just can't see what is wrong or what I am missing to check.

Hope you can help me.

Many thanks!

+3


source to share


1 answer


Maybe use utf8_encode ()



$content = utf8_encode("text dsdasasd"); 
$file = fopen($filename, 'w+');
fwrite($file, $content);
fclose($file);
echo $content; 

      

0


source







All Articles