Details about my Goal :
I Wanted to combine a new Data comes from my mobile application with the data which is stored on a Json file and used that data to process user things but any data greater than 1KB starts the error .
Error Codes : PHP Warning: fread(): Length parameter must be greater than 0
I’ve make sure the file size is not 0, its 4.89KB !
The Code :
$RawFile = fopen($FileName, "w+") or die("Unable to open file!");
$IsiFile = fread($RawFile,filesize($FileName));
$DataFile = json_decode($IsiFile, true);
$DataPengguna = $DataFile[$UserID];
$DataOverwrite = $DataPengguna . '' . $tulisan;
$DataFile[$UserID] = $DataOverwrite;
fwrite($RawFile, json_encode($DataFile));
fclose($RawFile);
The contents of Json file : Prnt.sc/r9hek8
Mean of the Variable in my code :
$IsiFile = The Json
$DataFile = I prefer Array to access the data
$DataPengguna = The user Data [Every user have their own UserID]
$DataOverwrite = Combine the old already stored data with the new data
Put it back on the array and do fwrite(), then closed the file
Advertisement
Answer
This is a place of a logical error: fopen($FileName, "w+")
The “w+” mode always drops a file content. Just use “r+” mode and if you need write to file later, reopen it with “w+” mode.