This is a public Forum  publicRSS

Topic

    Bibek Jena
    writting to csv file
    Topic posted August 21, 2018 by Bibek JenaJourneyer 
    106 Views, 2 Comments
    Title:
    writting to csv file
    Content:

    Hi ,

    i have a requirement where i need to fetch data from database and write into a csv file .

    I am not able to write any data into the csv file,csv file that i am getting is blank csv file.

    i have tried fpucsv() and fwrite () to write the data.

    i am trying this in file manager script.

    Attached the code which i have tried.

    Thanks.

     

    Version:
    v1.3
    Code Block:

    Answer

     

    • Vlad

      I used your script and it works fine for me, the CSV file contains the line in $fileContent.

      How are you accessing the /tmp/ directory to check the files?

      Here is the full script I used:

      <?php
      require_once(get_cfg_var("doc_root") . "/ConnectPHP/Connect_init.php");
      error_reporting(E_ALL);
      use RightNow\Connect\v1_3 as RNCPHP;
       
      initConnectAPI("username", "password");

      try {

      $sToday = date("Y-m-d H:i:s");
                  $target_path=sys_get_temp_dir();
                  $sFileName =$target_path."/"."Org_".$sToday.".csv";
                  $file="Org_".$sToday.".csv";
                  //header("Content-Type: application/csv");
                  //header("Content-Disposition:attachment;filename=".$sFileName);
                  echo($sFileName);
                      
                           $header=array();
                           $sFileOpen = fopen($sFileName, 'w');
                           
                           if(file_exists($sFileName))
                           {                    
      $fileContent="Account ID,Account Manager,Account Status,Account Type,AccountName,Account's Email Address,Address,Fax Phone,HQ,HierarchyGroup,Industry sector,Merged2AccountDNVGLID,Merged2AccountName,Mobile Phone,OEBS Id,Primary Lang,SalesCloudID,Sanction Status,SanctionComments,SanctionDate,Secondary Lang,SecondaryLanguage,SiebelAccountRowId,TaskId,Validation Status,VAT #,Website,Date Created,Date Last Updated\n";

                              fwrite($sFileOpen, $fileContent);                                        
                              fclose($sFileOpen);
                              
                           }
      }
      catch ( \Exception $err ){
          echo "<br><b>Exception</b>: line ".__LINE__.": ".$err->getMessage()."</br>";
      }
      ?>

    • João Pedro Rodrigues

      Hello Bibek,

      You must indicate in the function fopen exactly the directory that the file should be created. For example, I use the following function to create files and write some content:

      $fp = fopen("/vhosts/'yoursite'/euf/assets/themes/standard/files/data.csv", "w+");

      $write = fwrite($fp, "Hello World!" );

      fclose($fp);

      Using the code above, the file will be created in the '"Webdav" in /assets/themes/standard/files/.

      Hope this helps,

      João