Search code examples
javascriptphpfile-iohttp-status-codes

PHP write file works, but says false


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!


Solution

  • 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.