3

I want to create (write) a csv file in a specific folder. the name of this folder is attin_csv. In the image you can see where is located this folder. enter image description here

This is my code:

  $csv[0] = $header;
    //Creating attributes csv file
    $file = fopen("../attin_csv/attributes.csv","w+");
    foreach ($csv as $line){
        fputcsv($file,explode('%%',$line),"^","`");
    }
  • all of you give me the right answer, which of you should i marked as the solution? – Attila Naghi Nov 12 '14 at 10:17
0

Please use Mage::getBaseDir() which give you magento root dir.But it should be if you in magento file or you have using include Mage.php on your script.

then

$file = fopen("../attin_csv/attributes.csv","w+");

I should be like

$file = fopen(Mage::getBaseDir()."attin_csv".DS."attributes.csv","w+");

See more at http://alanstorm.com/magento_base_directories

  • it is working without DS. What is DS? – Attila Naghi Nov 12 '14 at 10:14
  • Ds give directory sepreator. '/' create issue on window system – Amit Bera Nov 12 '14 at 10:15
  • The base dir always already has a trailing slash – Fabian Schmengler Nov 12 '14 at 10:16
1

you can use like below

Also please add before

// set require path as you have present
    require 'app/Mage.php';
    $app = Mage::app('default');


$csv[0] = $header;
    //Creating attributes csv file
    $file = fopen(Mage::getBaseDir() . DS ."attin_csv/attributes.csv","w+");
    foreach ($csv as $line){
        fputcsv($file,explode('%%',$line),"^","`");
    }

DS : directory separate like '/' if working without it remove it.

1

This would be Mage::getBaseDir() . 'attin_csv/'

But if you have any influence on the location of this directory, you should follow the Magento conventions and move attin_csv into one of the following dirs:

  • media if the files stored there will be used for unlimited time
  • var if the file serves a temporary need (like var/cache, var/import or var/export)
0

I'd not use fopen and fputcsv directly since Magento has some methods that are easier to use ....

$path = Mage::getBaseDir() . 'attin_csv';
$file = 'attributes.csv';

Create export folder wither propper permissions ...

// create directory under var/export
$io = new Varien_Io_File();
$io->checkAndCreateFolder($path);

Write CSV file ...

$csv = new Varien_File_Csv();
$csv->saveData($path . DS. $file, $data);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.