donglu953744 2015-08-26 08:37
浏览 76

在模块配置面板Prestashop中上传文件

I'm trying to create a XML import module that will convert given file to CSV format and then use that CSV to import categories and products.

I have a working configuration page made with getContent() it basically calls a method that generates this form via $helper->generateForm(). $helper is a HelperForm() object.

protected function getConfigForm()
{
    return array(
        'form' => array(
            'legend' => array(
            'title' => $this->l('Settings'),
            'icon' => 'icon-cogs',
            ),
            'input' => array(
                array(
                    'type' => 'file',
                    'label' => $this->l('XML file'),
                    'name' => 'XMLIMPORT_XML_FILE',
                    'desc' => $this->l('Select file you wish to import.'),
                    'required' => true
                ),
                array(
                    'col' => 3,
                    'type' => 'text',
                    'prefix' => '<i class="icon icon-envelope"></i>',
                    'desc' => $this->l('Enter a valid email address'),
                    'name' => 'XMLIMPORT_LINES',
                    'label' => $this->l('Records per file'),
                ),
            ),
            'submit' => array(
                'title' => $this->l('Save'),
            ),
        ),
    );
}

I need to get this data to my XML converter. How do I upload a file (around 10-20MB) to Prestashop to be then able to do other stuff with it? How to save it permanently on the server?

I tried doing this:

return array(
    'XMLIMPORT_XML_FILE' => Configuration::get('XMLIMPORT_XML_FILE', null),
    'XMLIMPORT_LINES' => Configuration::get('XMLIMPORT_LINES', 1000)
);

And after that this:

$form_values = $this->getConfigFormValues(); // returned array from above

foreach (array_keys($form_values) as $key)
    Configuration::updateValue($key, Tools::getValue($key));

And later using my own class for XML conversion like this, hoping that it will give me file handle.

$xml_converter = new XMLToCSVConverter(Configuration::get('XMLIMPORT_XML_FILE'), 'output', 'example_products.php');

Apparently it didn't as nothing happens. The class itself is working fine outside of Prestashop module. The constructor is __construct($xml_file, $csv_filename, $template_file).

I need to pass the file I upload to that constructor. I've been struggling for days now.

@edit: I can see the contents of the file inside the HTTP call when submit is clicked. But how do I pass that file to my class?

  • 写回答

2条回答 默认 最新

  • dsq1982 2015-08-27 06:04
    关注

    As far as I remember 'type' => 'file', doesn't actually save any values in the database. This type is only meant to output a file field in your form.

    After submitting, you should then do you custom processing with $_FILES['XMLIMPORT_XML_FILE'] : move to upload/ or whereever you want.

    'XMLIMPORT_XML_FILE' => Configuration::get('XMLIMPORT_XML_FILE', null), won't return you anything. After uploading you my wish to save the uploaded file path here, but it won't show up next time in the form unless you build the output yourself.

    Module configratuon is meant to save text config values. Handling files is trickier and you have to do them yourself each time.

    EDIT:

    To intercept the saving process, look up the submit button name and make an if statement:

    public function getContent() {
        if(Tools::isSubmit('submitButtonName')) {
          error_log(print_r($_FILES, 1));
        }
    }
    

    There's probably a function postProcess which does the same (it looks like you copied the methods from a default module).

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?