Search code examples
phpfopencounterfwrite

PHP fopen script read line


I've created a small script that opens a text document, increments a number each time a user opens a specific page and writes the new number on the next line in the document. I would like the script to be able to read the last line of the document and increment that number. Currently, it continues to look at the first line and increment the first number entered.

function job_number_script(){
    $job_data = "";
    $user_info = $_SESSION['user_info'];
    $user_id = $user_info['user']['id'];
    $job_id = "";
    //Initialize the return
    $job_number_page = ("job_setup.txt");
    $job_number = file($job_number_page);
    $work = explode(",",$job_number[0]);
    $work[0]++;
    $job_id = $work[0]++;
    $work2 = array($job_id, $user_id);
    $works = implode(",",$work2);
    $fp = fopen($job_number_page, "a+");
    fputs($fp, "$works\n");
    fclose($fp);
    $job_data = $job_id;
    return $job_data;
}

Solution

  • one way could be that you just replace all occurrences of $work[0] with $work[count($work)-1].