0

I have a csv file named data1.csv. The first time i use php to read the csv file content I can get the contents correctly. The problem is I cannot read the new data after I update the data in data1.csv. It keeps returning the previous data. What could be the wrong?

    $file_handle = fopen($csvFile, 'r');
    if(!$file_handle) die("Can't open file");

    while (!feof($file_handle) ) {
      $line_of_text[] = fgetcsv($file_handle, 0, $csv_spliter);

    }

    clearstatcache();

    fclose($file_handle);
    $file_content = $line_of_text;
ovicko
  • 2,242
  • 3
  • 21
  • 37
  • Is the code from the read that works? Could you show the one that is not working and how you update the csv file? – Roland Starke Nov 05 '18 at 12:57
  • This code can read the csv content. The csv is updated manually – ovicko Nov 05 '18 at 13:00
  • Do you also get the old data if you completely delete the file? If it only affects new lines it could be a line ending problem. – Roland Starke Nov 05 '18 at 13:03
  • Does it work if you upload the new file as ``data2.csv`` and change your script to read that new file? If so, then it sure seems like the first file is being cached somewhere. If not, then it would not appear to be a caching problem. – kmoser Nov 05 '18 at 13:12
  • @RolandStarke I only get the old data, if i change the word `product` to `PRODUCT1`, on csv reading it will show `product`. Note: I am not adding any new columns/rows just updating already existing data – ovicko Nov 05 '18 at 13:13
  • 1
    did you check is data saved properly or not? – siddhesh Nov 05 '18 at 13:13
  • @kmoser if I change to data2.csv it works fine, i.e it shows the newly updated data – ovicko Nov 05 '18 at 13:16
  • What is the value of ``$csvFile``? – kmoser Nov 05 '18 at 13:18
  • @kmoser its a link to the `data1.csv` – ovicko Nov 05 '18 at 13:20
  • @ovicko Please tell me the exact value. Depending on whether it's a path to a local file, or begins with ``http://`` may affect this. – kmoser Nov 05 '18 at 13:30
  • @kmoser `$csvFile = 'https://example.com/feed/data1.csv'` – ovicko Nov 05 '18 at 13:34
  • 2
    @ovicko That's your problem right there. See this question: https://stackoverflow.com/questions/511476/does-phps-fopen-function-implement-some-kind-of-cache – kmoser Nov 05 '18 at 13:34

1 Answers1

1

Since you are retrieving data via http/https, it is most likely being cached. See Does PHPs fopen function implement some kind of cache?

kmoser
  • 8,780
  • 3
  • 24
  • 40
  • I have tried using solutions stated still no difference – ovicko Nov 05 '18 at 15:18
  • 1
    Did you try adding a unique query string to the URL every time you attempted to fetch it, e.g. ``https://example.com/feed/data1.csv?12345``? – kmoser Nov 06 '18 at 13:51
  • @ovicko If my answer works for you, please consider marking it as the accepted answer, thanks! – kmoser Nov 09 '18 at 00:51