dqfkd82886 2017-04-24 14:23
浏览 25
已采纳

PHP写文件有效,但说错误

I don't have much experience with PHP, but what I'm trying to do is to write content to a file. For some reason the content is written to the file, but still returns 'failed to write to file!' with the 400 status code. But the contents are successfully written to the file. How?

php code (update.php):

<?php
    //get root and page of request
    $content_root = $_SERVER['DOCUMENT_ROOT'] . '/Animagie/content';
    $page = $_POST['page'];

    //open the correct contentfile
    $content_file = fopen($content_root . '/' . $page, 'w');

    if (isset($_POST[$page . '-content'])) {

        if (fwrite($content_file, $_POST[$page . '-content']) === FALSE ) {

            echo 'failed to write to file!';
            fclose($content_file);
            http_response_code(400);
        } else {

            fclose($content_file);
            http_response_code(200);
        }
    } else {

        echo 'something went wrong!';
        fclose($content_file);
        http_response_code(400);
    }
?>

I call update.php with following code:

    editor.addEventListener('saved', function(e) {
    var name, payload, regions, xhr;

    //check if something changed
    regions = e.detail().regions;
    if (Object.keys(regions).length === 0) {
        return;
    }

    //set editor busy while saving
    this.busy(true);

    // Collect the contents of each region into a FormData instance
    payload = new FormData();
    payload.append('page', getCurrentPage());
    for (name in regions) {
        if (regions.hasOwnProperty(name)) {
            payload.append(name, regions[name]);
        }
    }

    // Send the updated content to the server to be saved
    function onStateChange(e) {
        //check if request is finished
        if (e.target.readyState === 4) {
            editor.busy(false);
            if (e.target.status === '200') {
                new ContentTools.FlashUI('ok');
            } else {
                new ContentTools.FlashUI('no');
            }
        }
    }

    xhr = new XMLHttpRequest();
    xhr.addEventListener('readystatechange', onStateChange);
    xhr.open('POST', '../api/update.php');
    xhr.send(payload);
});

As you probably can tell it's quiet important to get the correct statuscode since I check for it and return if it's succesfull or not to the user. Anyone able to help me?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • douzhouqin6223 2017-04-24 14:50
    关注

    Apperently the problem lays in the javascript check:

    if (e.target.status === '200') {
        new ContentTools.FlashUI('ok');
    } else {
        new ContentTools.FlashUI('no');
    }
    

    should be

    if (e.target.status == 200) {
        new ContentTools.FlashUI('ok');
    } else {
        new ContentTools.FlashUI('no');
    }
    

    Also after I switched the if-statement (like told by @Jon Stirling), postman wasn't refreshed yet. So it was partially a wrong if-statement on the server side & wrong if-statement on the client side.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型