Magento 2 Write to CSV within ControllerUnit Test for overwrite collection class in magento2CSV import not working - Need AssistanceI created a custom module ,but getting error, not able to figure out what the error is about. How to get out of this error?Magento 2.1: Invoke urlBuilder->getUrl() in a controller in a custom moduleMagento 2: How to override newsletter Subscriber modelMagento 2: Add a product to the cart programmaticallyMagento 2 Create dynamic array From different Model Collection to use in multi select in gridMagento 2.2.5: Overriding Admin Controller sales/orderMagento 2.3 Can't view module's front end page output?magento 2.2 trying to save multi select value in database

How to properly say asset/assets in German

13th chords on guitar

Single level file directory

Comment traduire « That screams X »

Can you actually break an FPGA by programming it wrong?

Put my student loan in parents’ second mortgage - help?

How did Lefschetz do mathematics without hands?

Will writing actual numbers instead of writing them with letters affect readership?

Why wasn't EBCDIC designed with contiguous alphanumeric characters?

Sharing referee/AE report online to point out a grievous error in refereeing

Grant dbcreator only for databases matching prefix

Can a nowhere continuous function have a connected graph?

How did researchers find articles before the Internet and the computer era?

Why do I need two parameters in an HTTP parameter pollution attack?

I need help with pasta

Reusable spacecraft: why still have fairings detach, instead of open/close?

How Do I Know When I am in Private Mode?

Is it okay to fade a human face just to create some space to place important content over it?

What kind of jet plane is this?

Is there reliable evidence that depleted uranium from the 1999 NATO bombing is causing cancer in Serbia?

Converting Geographic Coordinates into Lambert2008 coordinates

I just started should I accept a farewell lunch for a coworker I don't know?

Why would anyone even use a Portkey?

Copy group of files (Filename*) to backup (Filename*.bak)



Magento 2 Write to CSV within Controller


Unit Test for overwrite collection class in magento2CSV import not working - Need AssistanceI created a custom module ,but getting error, not able to figure out what the error is about. How to get out of this error?Magento 2.1: Invoke urlBuilder->getUrl() in a controller in a custom moduleMagento 2: How to override newsletter Subscriber modelMagento 2: Add a product to the cart programmaticallyMagento 2 Create dynamic array From different Model Collection to use in multi select in gridMagento 2.2.5: Overriding Admin Controller sales/orderMagento 2.3 Can't view module's front end page output?magento 2.2 trying to save multi select value in database






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have the following code:



protected $_fileSystem;
protected $_directoryList;

public function __construct(
MagentoFrameworkFilesystem $fileSystem,
MagentoFrameworkAppFilesystemDirectoryList $directoryList
)
$this->_fileSystem = $fileSystem;
$this->_directoryList = $directoryList;
parent::__construct($context);


public function execute()
$this->writeToCSV();


private function writeToCSV()

try
$media = $this->_fileSystem->getDirectoryWrite($this->_directoryList::MEDIA);
$media->writeFile('text.txt', 'test');

catch(Exception $e)
echo($e->getMessage());





However, this doesn't seem to be writing anything to the text file that exists within the same folder as the controller.










share|improve this question




























    1















    I have the following code:



    protected $_fileSystem;
    protected $_directoryList;

    public function __construct(
    MagentoFrameworkFilesystem $fileSystem,
    MagentoFrameworkAppFilesystemDirectoryList $directoryList
    )
    $this->_fileSystem = $fileSystem;
    $this->_directoryList = $directoryList;
    parent::__construct($context);


    public function execute()
    $this->writeToCSV();


    private function writeToCSV()

    try
    $media = $this->_fileSystem->getDirectoryWrite($this->_directoryList::MEDIA);
    $media->writeFile('text.txt', 'test');

    catch(Exception $e)
    echo($e->getMessage());





    However, this doesn't seem to be writing anything to the text file that exists within the same folder as the controller.










    share|improve this question
























      1












      1








      1








      I have the following code:



      protected $_fileSystem;
      protected $_directoryList;

      public function __construct(
      MagentoFrameworkFilesystem $fileSystem,
      MagentoFrameworkAppFilesystemDirectoryList $directoryList
      )
      $this->_fileSystem = $fileSystem;
      $this->_directoryList = $directoryList;
      parent::__construct($context);


      public function execute()
      $this->writeToCSV();


      private function writeToCSV()

      try
      $media = $this->_fileSystem->getDirectoryWrite($this->_directoryList::MEDIA);
      $media->writeFile('text.txt', 'test');

      catch(Exception $e)
      echo($e->getMessage());





      However, this doesn't seem to be writing anything to the text file that exists within the same folder as the controller.










      share|improve this question














      I have the following code:



      protected $_fileSystem;
      protected $_directoryList;

      public function __construct(
      MagentoFrameworkFilesystem $fileSystem,
      MagentoFrameworkAppFilesystemDirectoryList $directoryList
      )
      $this->_fileSystem = $fileSystem;
      $this->_directoryList = $directoryList;
      parent::__construct($context);


      public function execute()
      $this->writeToCSV();


      private function writeToCSV()

      try
      $media = $this->_fileSystem->getDirectoryWrite($this->_directoryList::MEDIA);
      $media->writeFile('text.txt', 'test');

      catch(Exception $e)
      echo($e->getMessage());





      However, this doesn't seem to be writing anything to the text file that exists within the same folder as the controller.







      magento2 controllers csv






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 18 at 11:26









      A. FletcherA. Fletcher

      9410 bronze badges




      9410 bronze badges




















          1 Answer
          1






          active

          oldest

          votes


















          2














          You can write csv file using below way,



          <?php

          protected $filesystem;
          protected $directoryList;
          protected $csvProcessor;

          public function __construct(
          MagentoFrameworkFileCsv $csvProcessor,
          MagentoFrameworkAppFilesystemDirectoryList $directoryList,
          MagentoFrameworkFilesystem $filesystem
          )

          $this->filesystem = $filesystem;
          $this->directoryList = $directoryList;
          $this->csvProcessor = $csvProcessor;


          function writeToCsv()
          $fileDirectoryPath = $this->directoryList->getPath(MagentoFrameworkAppFilesystemDirectoryList::VAR_DIR);

          if(!is_dir($fileDirectoryPath))
          mkdir($fileDirectoryPath, 0777, true);
          $fileName = 'export.csv';
          $filePath = $fileDirectoryPath . '/' . $fileName;

          $data = [];
          /* pass data array to write in csv file */
          $data[] = ['orderid' => '100001'];
          $this->csvProcessor
          ->setEnclosure('"')
          ->setDelimiter(',')
          ->saveData($filePath, $data);

          return true;



          You can check your generated CSV file inside var folder






          share|improve this answer

























          • I'll give it a whirl. Thanks again Rakesh, always super fast at answering questions.

            – A. Fletcher
            Jan 18 at 11:32











          • Just a quick question about the directory path. Do I carry on from MagentoFrameworkApp as though I was in the app folder? So in my case codecompanymodule?

            – A. Fletcher
            Jan 18 at 11:38











          • my path is var directory you can set your path

            – Rakesh Donga
            Jan 18 at 11:42













          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "479"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f258349%2fmagento-2-write-to-csv-within-controller%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You can write csv file using below way,



          <?php

          protected $filesystem;
          protected $directoryList;
          protected $csvProcessor;

          public function __construct(
          MagentoFrameworkFileCsv $csvProcessor,
          MagentoFrameworkAppFilesystemDirectoryList $directoryList,
          MagentoFrameworkFilesystem $filesystem
          )

          $this->filesystem = $filesystem;
          $this->directoryList = $directoryList;
          $this->csvProcessor = $csvProcessor;


          function writeToCsv()
          $fileDirectoryPath = $this->directoryList->getPath(MagentoFrameworkAppFilesystemDirectoryList::VAR_DIR);

          if(!is_dir($fileDirectoryPath))
          mkdir($fileDirectoryPath, 0777, true);
          $fileName = 'export.csv';
          $filePath = $fileDirectoryPath . '/' . $fileName;

          $data = [];
          /* pass data array to write in csv file */
          $data[] = ['orderid' => '100001'];
          $this->csvProcessor
          ->setEnclosure('"')
          ->setDelimiter(',')
          ->saveData($filePath, $data);

          return true;



          You can check your generated CSV file inside var folder






          share|improve this answer

























          • I'll give it a whirl. Thanks again Rakesh, always super fast at answering questions.

            – A. Fletcher
            Jan 18 at 11:32











          • Just a quick question about the directory path. Do I carry on from MagentoFrameworkApp as though I was in the app folder? So in my case codecompanymodule?

            – A. Fletcher
            Jan 18 at 11:38











          • my path is var directory you can set your path

            – Rakesh Donga
            Jan 18 at 11:42















          2














          You can write csv file using below way,



          <?php

          protected $filesystem;
          protected $directoryList;
          protected $csvProcessor;

          public function __construct(
          MagentoFrameworkFileCsv $csvProcessor,
          MagentoFrameworkAppFilesystemDirectoryList $directoryList,
          MagentoFrameworkFilesystem $filesystem
          )

          $this->filesystem = $filesystem;
          $this->directoryList = $directoryList;
          $this->csvProcessor = $csvProcessor;


          function writeToCsv()
          $fileDirectoryPath = $this->directoryList->getPath(MagentoFrameworkAppFilesystemDirectoryList::VAR_DIR);

          if(!is_dir($fileDirectoryPath))
          mkdir($fileDirectoryPath, 0777, true);
          $fileName = 'export.csv';
          $filePath = $fileDirectoryPath . '/' . $fileName;

          $data = [];
          /* pass data array to write in csv file */
          $data[] = ['orderid' => '100001'];
          $this->csvProcessor
          ->setEnclosure('"')
          ->setDelimiter(',')
          ->saveData($filePath, $data);

          return true;



          You can check your generated CSV file inside var folder






          share|improve this answer

























          • I'll give it a whirl. Thanks again Rakesh, always super fast at answering questions.

            – A. Fletcher
            Jan 18 at 11:32











          • Just a quick question about the directory path. Do I carry on from MagentoFrameworkApp as though I was in the app folder? So in my case codecompanymodule?

            – A. Fletcher
            Jan 18 at 11:38











          • my path is var directory you can set your path

            – Rakesh Donga
            Jan 18 at 11:42













          2












          2








          2







          You can write csv file using below way,



          <?php

          protected $filesystem;
          protected $directoryList;
          protected $csvProcessor;

          public function __construct(
          MagentoFrameworkFileCsv $csvProcessor,
          MagentoFrameworkAppFilesystemDirectoryList $directoryList,
          MagentoFrameworkFilesystem $filesystem
          )

          $this->filesystem = $filesystem;
          $this->directoryList = $directoryList;
          $this->csvProcessor = $csvProcessor;


          function writeToCsv()
          $fileDirectoryPath = $this->directoryList->getPath(MagentoFrameworkAppFilesystemDirectoryList::VAR_DIR);

          if(!is_dir($fileDirectoryPath))
          mkdir($fileDirectoryPath, 0777, true);
          $fileName = 'export.csv';
          $filePath = $fileDirectoryPath . '/' . $fileName;

          $data = [];
          /* pass data array to write in csv file */
          $data[] = ['orderid' => '100001'];
          $this->csvProcessor
          ->setEnclosure('"')
          ->setDelimiter(',')
          ->saveData($filePath, $data);

          return true;



          You can check your generated CSV file inside var folder






          share|improve this answer















          You can write csv file using below way,



          <?php

          protected $filesystem;
          protected $directoryList;
          protected $csvProcessor;

          public function __construct(
          MagentoFrameworkFileCsv $csvProcessor,
          MagentoFrameworkAppFilesystemDirectoryList $directoryList,
          MagentoFrameworkFilesystem $filesystem
          )

          $this->filesystem = $filesystem;
          $this->directoryList = $directoryList;
          $this->csvProcessor = $csvProcessor;


          function writeToCsv()
          $fileDirectoryPath = $this->directoryList->getPath(MagentoFrameworkAppFilesystemDirectoryList::VAR_DIR);

          if(!is_dir($fileDirectoryPath))
          mkdir($fileDirectoryPath, 0777, true);
          $fileName = 'export.csv';
          $filePath = $fileDirectoryPath . '/' . $fileName;

          $data = [];
          /* pass data array to write in csv file */
          $data[] = ['orderid' => '100001'];
          $this->csvProcessor
          ->setEnclosure('"')
          ->setDelimiter(',')
          ->saveData($filePath, $data);

          return true;



          You can check your generated CSV file inside var folder







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jun 20 at 6:54









          Khushbu

          30713 bronze badges




          30713 bronze badges










          answered Jan 18 at 11:30









          Rakesh DongaRakesh Donga

          2,9456 silver badges21 bronze badges




          2,9456 silver badges21 bronze badges












          • I'll give it a whirl. Thanks again Rakesh, always super fast at answering questions.

            – A. Fletcher
            Jan 18 at 11:32











          • Just a quick question about the directory path. Do I carry on from MagentoFrameworkApp as though I was in the app folder? So in my case codecompanymodule?

            – A. Fletcher
            Jan 18 at 11:38











          • my path is var directory you can set your path

            – Rakesh Donga
            Jan 18 at 11:42

















          • I'll give it a whirl. Thanks again Rakesh, always super fast at answering questions.

            – A. Fletcher
            Jan 18 at 11:32











          • Just a quick question about the directory path. Do I carry on from MagentoFrameworkApp as though I was in the app folder? So in my case codecompanymodule?

            – A. Fletcher
            Jan 18 at 11:38











          • my path is var directory you can set your path

            – Rakesh Donga
            Jan 18 at 11:42
















          I'll give it a whirl. Thanks again Rakesh, always super fast at answering questions.

          – A. Fletcher
          Jan 18 at 11:32





          I'll give it a whirl. Thanks again Rakesh, always super fast at answering questions.

          – A. Fletcher
          Jan 18 at 11:32













          Just a quick question about the directory path. Do I carry on from MagentoFrameworkApp as though I was in the app folder? So in my case codecompanymodule?

          – A. Fletcher
          Jan 18 at 11:38





          Just a quick question about the directory path. Do I carry on from MagentoFrameworkApp as though I was in the app folder? So in my case codecompanymodule?

          – A. Fletcher
          Jan 18 at 11:38













          my path is var directory you can set your path

          – Rakesh Donga
          Jan 18 at 11:42





          my path is var directory you can set your path

          – Rakesh Donga
          Jan 18 at 11:42

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f258349%2fmagento-2-write-to-csv-within-controller%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

          Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

          Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?