0

Hi everyone. I have an application where users submit text files and then based on some rules, the application gives instant responses of whether the submitted text file is right or wrong.
What actually happens behind the scenes is that I have the correct text file on the server and then I simply compare the submitted text file line by line against it.
The problem now is that files submitted from Windows OS are marked as incorrect though when opening the file in a text editor and comparing to the file on the server they are similar.
I have also carried some tests on Linux using the diff tool and it actually shows there's a difference in all the lines yet to the eye there's none.

Thanks!

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by sureronald
0

Just some more details. This is a portion of the code that prepares the new lines for comparison

$answer_sheet = file_get_contents($md['test_file.txt']);
$answer_sheet_lines=explode("\n",$answer_sheet);

t

0

I am no expert and this might not be the answer you are looking for. But, I thought I'd share this knowledge.

To my knowledge, line ending convention may be different in different OS platforms. auto_detect_line_endings allows you to check which convention the data in the file is using when you use fgets function to read the lines. It might help you to check out these functions.

Cheers.

0

I've eventually solved it. I have simply used the fgets function to read the lines one by one and then return an array instead of file_get_contents and then exploding at new lines as in the code upthread.
Here's my implementation

function make_lines($file_path){
    $lines = array();
    $handle = fopen($file_path,'r');
    if($handle){
        while(($buffer = fgets($handle))!= false){
            $lines[] = trim($buffer);
        }
    }
    fclose($handle);
    
    return $lines;
}

Many thanks!!!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.