0

Newbie here with a frustrating, but probably simple question. I am trying to write a script that will get the last line of a .csv with 2 columns. I only need the value in the second column to do a calculation. I have been trying to use str_getcsv to access the second column and then calculate the math. The script works fine on my local server but when I upload to the webserver the script stops and the rest of the page does not load. There are no error messages. Here is the code that I have been working with. It gets to the hello1 echo and stops.

$file = "test.csv"; 
//echo "hello";
$data = file($file);
$line = $data[count($data)-1];
echo "hello1";
$dump = str_getcsv($line);
echo "hello2";
$mynum = $dump[1]*1440;
echo $mynum;
Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71

2 Answers2

1

http://php.net/manual/en/function.str-getcsv.php

(PHP 5 >= 5.3.0)

What PHP version is installed on your host?

There are no error messages.

Then enable error reporting.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Thanks Neal and Code, it was in fact a php version issue on the server and reporting was not all on... – CrossFunction Nov 22 '11 at 01:27
  • 1
    Thanks all, with your input I was able lookup a snippet code that checked for the str_getcsv function, and if it is not found to use an alternative. Its all working now. I simply added this code above the original. if(!function_exists('str_getcsv')) { function str_getcsv($input, $delimiter = ",", $enclosure = '"', $escape = "\\") { $fp = fopen("php://memory", 'r+'); fputs($fp, $input); rewind($fp); $data = fgetcsv($fp, null, $delimiter, $enclosure); // $escape only got added in 5.3.0 fclose($fp); return $data; } } – CrossFunction Nov 22 '11 at 01:32
0
  • Enable all php error on your server.

  • Make sure that test.csv is readable.

Naftali
  • 144,921
  • 39
  • 244
  • 303