0

I'm trying to create a PHP page that opens the query and automatically updates the csv file with a new value.

I disc location I have file "graph.csv" with date, and COUNT from SQL Query. I try open this file and add new row everytime when page is loaded. But, currently browser try save and overwrite file manual. php and csv file are in the same location.

 <?php  

      $connect = oci_connect("login","pass","db");  
      header('Content-Type: text/csv; charset=utf-8');  
      header('Content-Disposition: attachment; filename=graph.csv');  
      $output = fopen("php://output", "w");  
      fputcsv($output, array('TIME','Orders'));  
      $query = "SELECT column1, column2 FROM table1";  
      $result = oci_parse($connect, $query);
      $r=oci_execute($result);    
      while ($row = oci_fetch_array($result, OCI_BOTH))  
      {
            $handle = fopen("graph.csv", "a");      

            $handle = fputcsv($output, $row);  
      }  
      $handle = fclose($output);  


 ?>  
user9557556
  • 43
  • 1
  • 5
  • _“But, currently browser try save and overwrite file manual”_ - you can not automatically update a file that was saved as a download by the browser. You can only do this on the server side of things, append to an already existing file there. – misorude Nov 05 '18 at 12:39
  • what are the headers for? and why fopen("php://output") ?? `$handle = fclose($output);` ? – Jeff Nov 05 '18 at 12:40
  • _sidenote:_ the query looks invalid here: `column2, FROM` – Jeff Nov 05 '18 at 12:41
  • This sounds like you might need to read & understand what is explained in https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming first of all. – misorude Nov 05 '18 at 12:41
  • 2
    To me it looks like a copy & paste of code from two different sources mixed together. It's not clear what you want to achieve (at least not to me) – Jeff Nov 05 '18 at 12:44
  • It should get data only from SQL and save to file :( – user9557556 Nov 05 '18 at 12:49
  • Save it to a file _where_? On the server? Then this code is the wrong one to begin with, this triggers a download of data to the client. Or on the client side? Again, that is not possible - you can not “append” to a file I downloaded previously via my browser, you can only trigger a completely new download, giving me the option to overwrite my existing file with that if I so chose. – misorude Nov 05 '18 at 12:51
  • Ok, and if I run this php file by server side? – user9557556 Nov 05 '18 at 12:52
  • You always run PHP on the server side when it comes to web applications. If you want to write into a file on the server side, then do that - that does not involve setting any headers or using `php://output` to begin with. – misorude Nov 05 '18 at 12:55

0 Answers0