-1

I have a problem.

my class.log.php

<?php
 
date_default_timezone_set('Europe/Istanbul');
 
class Log
{
    public function add($uID, $pID, $text)
    {
        $date = date("d.m.Y") . " " . date("H:i:s");
        $data = array("Date" => $date, "UserID" => $uID, "ProductID" => $pID, "Text" => $text);
        
        $folder = "../log/";
        $logFileName = $folder . date("d-m-Y") . ".log";
 
        if (!file_exists($folder)) {
            mkdir($folder);
        }
 
        if (!file_exists($logFileName)) {
            file_put_contents($logFileName, "\xEF\xBB\xBF");
        }
 
        $ofile = fopen($logFileName, "a");
 
        fwrite($ofile, $data);
        fclose($ofile);
    }
}
?>

I use it function like here in pages.

require_once "class.log.php";
$log = new Log();
$log->add('41','16','Product Added');

why can't I print output in json format to my .log file. I can't find where is the error in the codes

2
  • 2
    Because you try to write an array, what is not possible. Change to fwrite($ofile, json_encode($data));. Commented Jun 20, 2022 at 17:45
  • 1
    You can only write JSON if you have some JSON to write Commented Jun 20, 2022 at 17:46

1 Answer 1

0

when write to file you should convert array to string

use this code

    fwrite($ofile, json_encode($data,JSON_UNESCAPED_UNICODE));
Sign up to request clarification or add additional context in comments.

2 Comments

That is not "converting an array to a string" that is "encoding the data as JSON".
you say correct , but type of result is string. you can check result with var_dump.@Sammitch

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.