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!

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

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.

Thanks let me check it out and get back

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!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.