Search code examples
jqueryfile-iophppolling

Problems with php File I/O and ajax


I'm trying to polling with ajax and php for an app Im writing. The php essentially receives an stringified object to put into a file, that can be read later. I have the writing to objects working just fine. The problem I have right now is that if the php tries to read a file that doesn't exist, it seems to return an error code to the $.post() call. I want it to return JSON, and success, so that I can process is a bit differently.

The php in question is as follows:

public function read(){
        $id = $this->input->post('id');
        $test = fopen("./meetings/".$id.".mt", 'r');
        if(!$test){
            error_log("Unable to open file, you mutt!");
            echo json_encode(array('status' => "FAIL"));
        }else{
        error_log("Here! 2");
        $obj = array();
            while(!feof($test)){
                error_log("Here! 3");
                $tmp = fgets($test);
                error_log("Here! 4");
                if($tmp){
                    $obj[count($obj)] = $tmp;
                }
            }
            fclose($test);
            error_log("Here! 5");
        if(!count($obj) > 0){
            echo json_encode(array('status' => "FAIL"));
        }
        echo json_encode(array('status' => 'OKAY', 'obj' => $obj));
        }
    }

and the Javascript(using jquery) is as follows:

function read(){
        $.post('<?php echo base_url(); ?>meetings/read', {id: <?php echo $id;?>}, function(json){
            console.log(json.status);
            if(json.status == 'OKAY'){
                for(var i = 0 ; i < json.obj.length ; i++){
                    parseObject(json.obj[i]);
                }
            }
        }, 'json');
    }

the javascript doesn't get to the success function, I checked using ajaxError() but I'm not sure how to fix this, or even why the php is sending an error cause I think I'm ckecking for it. The php works as expected otherwise. also, the polling is indeed calling the server as frequently as it should be. Any suggestions on fixing this? If you need any more information please just ask. Thanks!


Solution

  • public function read(){
       $id = $this->input->post('id');
    
       /* changes */
       $path = "./meetings/".$id.".mt";
       if (!file_exists($path)){
         echo json_encode(array('status' => "FAIL")); 
         return;// this exit from here and go to js file
       }
       /* changes ends */
    
        $test = fopen($path, 'r');
        if(!$test){
            error_log("Unable to open file, you mutt!");
            echo json_encode(array('status' => "FAIL"));
        }else{
        error_log("Here! 2");
        $obj = array();
            while(!feof($test)){
                error_log("Here! 3");
                $tmp = fgets($test);
                error_log("Here! 4");
                if($tmp){
                    $obj[count($obj)] = $tmp;
                }
            }
            fclose($test);
            error_log("Here! 5");
        if(!count($obj) > 0){
            echo json_encode(array('status' => "FAIL"));
        }
        echo json_encode(array('status' => 'OKAY', 'obj' => $obj));
        }
    }
    

    I tried your code using fixed constant and it worked. If still facing error wrap 'fopen' in try-catch block.In other case check path settings. Use firebug to see actual JS generated.use that if stucked.