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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题