0

The above mentioned problem occurs only sometimes. I have no idea why but I assume that my php script for saving and loadinf a JSON object in a JSON-file is not perfectly done.

writeLanguage.php

<?php
$myFile = "languages.json";

$cmsData = $_POST['data'];
$obj = json_decode($cmsData, true);

$myFile = "language.json";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh,json_encode($obj));

fclose($fh);

readLanguage.php

$cmsData = $_GET['data'];

$myFile = "language.json";
$fhh = fopen($myFile, 'r') or die("can't open file");

$jsondata = file_get_contents($myFile);
fclose($fhh);
$json = json_decode($jsondata, true);
echo $jsondata;

here my javascript code:

DataService.prototype.loadLanguagePromise = function () {
    return new Promise(function (resolve, reject) {
        $.ajax({
            url: "php/services/readLanguage.php",
            type: "GET",
            async: true,
            dataTyp: 'json',
            success: function (data) {
                resolve("stuff worked");
            },
            error: function (xhr, desc, err) {
                reject(Error("It broke"));
            }
        })
    })
};

DataService.prototype.saveLanguage = function (cmsObject) {
    return new Promise(function (resolve, reject) {
        $.ajax({
            url: "php/services/writeLanguage.php",
            type: "POST",
            data: {data: JSON.stringify(cmsObject)},
            dataTyp: 'json',
            success: function (data) {

                resolve(data);
            },
            error: function (xhr, desc, err) {
                reject(xhr, desc, err);
            }
        })
    })
};

I looked up for the definiton of Segmentation fault but could not really get an "aaaaah... of course, that's why".

moody
  • 404
  • 7
  • 19

2 Answers2

0

Try to delete fopen, fwrite and fclose. In first case you need only file_put_contents(), in second - ONLY file_get_contents.

<?php
$myFile = "languages.json";
$cmsData = $_POST['data'];
$obj = json_decode($cmsData, true);
$fh = file_put_contants($myFile, $cmsData,LOCK_EX) or die("can't open file");
$cmsData = $_GET['data'];
$myFile = "language.json";
$jsondata = file_get_contents($myFile);
$json = json_decode($jsondata, true);
echo $jsondata;
Maksim
  • 2,653
  • 2
  • 13
  • 28
  • 1
    You should also consider using the `LOCK_EX`-flag for `file_put_contents()` to get an exclusive lock on the file while writing to it. – M. Eriksson Mar 25 '19 at 22:40
0

The systemic analysis of the Segmentation fault you can do using answer at https://stackoverflow.com/a/7752606/4625150

The above one will take some time for you to figure out, however you will get proper root cause and then solve it.

Looking at your code, you are doing reading JSON and then writing JSON to file systems. That is a fault area and many times due to insufficient memory. To solve memory issue

  • Find your correct php.ini
  • Then increase memory by changing memory_limit in php.ini.
Susil Parida
  • 382
  • 2
  • 4