php Segmentation fault:写入或读取/加载文件时11

时间:2019-03-25 22:22:06

标签: php json segmentation-fault

上述问题仅在某些时候出现。我不知道为什么,但是我假设我的用于在JSON文件中保存和加载JSON对象的php脚本没有完美完成。

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;

这是我的JavaScript代码:

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);
            }
        })
    })
};

我一直在寻找分段错误的定义,但无法真正得到“ aaaaah ...当然,这就是原因”。

1 个答案:

答案 0 :(得分:0)

尝试删除fopen,fwrite和fclose。在第一种情况下,您仅需要file_put_contents(),在第二种情况中-仅需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;