1

I used fgetcsv() to extract some data from a csv file. Here's the code:

<?php
$csv = fopen("csv/contracts.csv","r");
while(!feof($csv)){
    $line = fgetcsv($csv,1024);
    if($line[0] == '')
        continue;
?>
    <li><?php echo "$line[2] - $line[3]"; ?></li>
<?php
}
?>

This works perfectly in my local machine but it becomes a disaster when I upload it on my server! I passed $delimiter with "," to fgetcsv() but nothing changed!
What's wrong?
PHP Version:
On local: 5.3.5
On server: 5.2.17

[EDIT]
My local output is like this:

s1 - t1
s2 - t2
s3 - t3
s4 - t4

But on server:

-
-
s3 - 
s4 -
Pedram Behroozi
  • 2,447
  • 2
  • 27
  • 48
  • 1
    Please define 'disaster' - what exactly is going wrong? – Quasdunk Nov 12 '11 at 09:46
  • Just an idea: The hyphen might be treated as a subtraction-operator. Have you tried this: ``? – Quasdunk Nov 12 '11 at 09:53
  • I tried. No luck. Thanks anyway – Pedram Behroozi Nov 12 '11 at 09:57
  • 1
    It also might have something to do with line endings (especially, if you're working on windows locally and upload it to a linux server). Have a look at this: http://stackoverflow.com/q/2321064/690897 and some related posts, maybe you'll find something there. Another frequent problem seems to be things like `\"` in the CSV. – Quasdunk Nov 12 '11 at 10:01
  • Hmmm... Nice guess. I add `ini_set('auto_detect_line_endings',TRUE);` but didn't solve the problem. I also viewed my csv file with cPanel on server. It seems all right! – Pedram Behroozi Nov 12 '11 at 10:13
  • Could I suggest moving fgetcsv into the `while` loop condition. `while($line=fgetcsv("file.csv",1024,',') !== FALSE) {`. Let the fgetcsv move the file pointer along rather than `feof`. And a good idea is to always include the delimeter! – David Barker Nov 12 '11 at 10:24

1 Answers1

0

i think u tested the code locally in windows machine and the serve is linux. The file format in linux and windows are different . The end of line as well as some other things differ in linux from windows . Do u test your code in linux ( as ur server is linux ) and then try in server. i think it will work fine there...

Akhil Thayyil
  • 9,263
  • 6
  • 34
  • 48