Ошибка сегментации php: 11 при записи или чтении / загрузке файла - PullRequest
1 голос
/ 26 марта 2019

Вышеупомянутая проблема возникает только иногда. Я понятия не имею, почему, но я предполагаю, что мой php-скрипт для сохранения и загрузки JSON-объекта в JSON-файл выполнен не совсем идеально.

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

Я нашел определение Ошибка сегментации , но не смог получить "ааааа ... конечно, вот почему".

1 Ответ

0 голосов
/ 26 марта 2019

Попробуйте удалить 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;
...