kaboom
kaboom

Reputation: 833

save file in utf-8 without bom

I have a form in Vietnamese and it submits data to a web service. This web service saves that data in a file. But the file always contains "0000" and nothing else...

Whats the correct way to save data encoding in utf-8 without bom? Thank you

This is my webservice.php:

if ($_SERVER["REQUEST_METHOD"] == "POST"){
    $data = $_POST["author"] . "\n" . $_POST["title"] . "\n" + $_POST["category"] . "\n" + $_POST["article"];
    $fileName = mb_convert_encoding($_REQUEST["author"], "UTF-8", "auto");
    $data = mb_convert_encoding($data, 'UTF-8', "auto");
    file_put_contents($fileName, $data, FILE_APPEND | LOCK_EX);
    print $data;
}else{
    invalidRequest();
} ?>

Upvotes: 0

Views: 3939

Answers (2)

Dr.Molle
Dr.Molle

Reputation: 117314

You are missing the filename-argument for file_put_contents() , your code will not save anything into a file.


<edit>

Also take a look at this:

  + $_POST["category"] . "\n" + $_POST["article"];
  ^                           ^

you are using the mathematical operators + there, so your string will be converted to a Number(0 in that case)

Upvotes: 3

Themroc
Themroc

Reputation: 495

Your form should contain

<form ... accept-charset="UTF-8">

Then you can write to file without mb_convert_encoding(). But NEVER EVER USE

$fileName = $_REQUEST["author"];

! Something like

$fileName = $author_names[$_REQUEST["author_id"]];

should be safe.

Upvotes: 2

Related Questions