Search code examples
phpcontrollerprestashopsmarty

Upload CSV File in Admin Controller - Custom module


I'm trying to create an Admin Controller with a csv file uploader to process it like an array.

I can't find an efficient way to do it, I tried to use $this-> fields_form, but nothing is showing up.

Then I did a tpl file with an input file, called in initContent, but I don't know how to retrieve my file in the controller.

I need to create multiple object of different classes that I made, thanks to the csv file.

Does somebody have some documentation that could help me, I've already search through prestashop dev doc, stack overflow, ect but I've didn't see anything that could help me (maybe I didn't search the good way ?)

Waiting for your help guys !

Update :

Update :

Just found a way to upload my file, but it's upload as .tmp and can't be processed as a csv file, how can I convert a tmp file to a csv ?

Here is my code :

public function __construct()
    {
        parent::__construct();
        // Base
        $this->bootstrap = true; // use Bootstrap CSS
        $this->fields_options = array(
            'general' => array(
                'title' => $this->l('Upload DB'),
                'fields' => array(
                    'DB_BULB_DATA' => array(
                        'title' => $this->l('Upload DB'),
                        'type' => 'file',
                        'name' => 'DB_BULB_DATA'
                    ),
                ),
                'submit' => array('title' => $this->trans('Save', array(), 'Admin.Actions')),
            ),
        );
        if(isset($_FILES['DB_BULB_DATA'])){
            $headers = fgetcsv(fopen($_FILES['DB_BULB_DATA']['tmp_name'], "r+"));
            print_r($headers);
        }
    }

Solution

  • Just find out how to do it, I feel dummy 😅

    I just needed to save my tmp file as a csv to be able to use it then.

    Here is the full code :

        <?php
    
    
    class Admin<YourModuleName>Upload<YourThings>DatabaseController extends ModuleAdminController
    {
        public function __construct()
        {
            parent::__construct();
            // Base
            $this->bootstrap = true; // use Bootstrap CSS
            $this->name = "Admin<YourModuleName>Upload<YourThings>Database";
            $this->fields_options = array(
                'general' => array(
                    'title' => $this->l('Upload DB'),
                    'fields' => array(
                        'DB_<YourThings>_DATA' => array(
                            'title' => $this->l('Upload DB'),
                            'type' => 'file',
                            'name' => 'DB_<YourThings>_DATA'
                        ),
                    ),
                    'submit' => array('title' => $this->l('Save')),
                ),
            );
        }
    
        public function initContent()
        {
            parent::initContent();
            unset($_FILES);
        }
    
        public function postProcess()
    
        {
            $fileName = '<YourThings>Db.csv';
            if (!file_exists(_PS_MODULE_DIR_ . '<YourModuleName>/upload/' . $fileName)) {
                if (isset($_FILES['DB_<YourThings>_DATA'])) {
                    $tmpPath = $_FILES['DB_<YourThings>_DATA']["tmp_name"];
                    move_uploaded_file($tmpPath, _PS_MODULE_DIR_ . '<YourModuleName>/upload/' . $fileName);
                    $uploadCsv = file(_PS_MODULE_DIR_ . '<YourModuleName>/upload/' . $fileName, FILE_SKIP_EMPTY_LINES);
                    $Db = array_map("str_getcsv", $uploadCsv, array_fill(0, count($uploadCsv), ';'));
                    $keys = array_shift($Db);
                    foreach ($Db as $i => $row) {
                        $Db[$i] = array_combine($keys, $row);
                    }
                    print_r($Db);
                }
            } else {
                $uploadCsv = file(_PS_MODULE_DIR_ . '<YourModuleName>/upload/' . $fileName, FILE_SKIP_EMPTY_LINES);
                $Db = array_map("str_getcsv", $uploadCsv, array_fill(0, count($uploadCsv), ';'));
                $keys = array_shift($Db);
                foreach ($Db as $i => $row) {
                    $Db[$i] = array_combine($keys, $row);
                }
                print_r($Db);
            }
            unset($_FILES['DB_<YourThings>_DATA']);
        }
    }