Jiess
Jiess

Reputation: 89

PHP - Cant save string variable to text file, gets corrupted

New to PHP and im reading a textfile into a string variable, removing double quotes and writing the contents back to a textfile. The problem is that the textfile, after saving, just consists of junk like this:

^@2^@0^@1^@0^@-^@1^@1^@-^@2^@0^@ ^@1^@3^@:^@5^@5^@,^@^@H^@K^@L^@M^@\^@S^@o^@f...

I am obviously doing something very wrong here.. Here is the code:

function replacequotes("myCommaSeparatedFile.csv")
{
    $rfile = fopen($filename, "r") or die("Unable to open file!");
    $outputfile = fopen("test2.csv", "w") or die("Unable to open file!");

    $readtext = fread($rfile, filesize($filename));
    $textoutput = str_replace('"', '', $readtext);

    echo $textoutput; // <- this shows ok on screen

    fwrite($outputfile, $textoutput);
    fclose($inpufile);
    fclose($outputfile);
}

Can someone help me out please, thanks.

Upvotes: 0

Views: 227

Answers (1)

Jiess
Jiess

Reputation: 89

Problem solved. dos2unix was a useful command. Thanks again Ali for the code cleaning!

Upvotes: 1

Related Questions