【问题标题】:Module Prestashop 1.7 : Custom image upload always replaced模块 Prestashop 1.7:自定义图像上传总是被替换
【发布时间】:2020-02-21 17:51:43
【问题描述】:

如果我不选择它,我的 file_url 字段总是会在数据库中被删除。 (即使图像已经集成)

如果我在这种情况下单击保存,则会删除字段 PC Image。

这是我 AdminCustomController 中的 postImage() 方法

 protected function postImage($id)
    {


        $file = isset($_FILES['file_url']) ? $_FILES['file_url'] : false;



        if ($file && is_uploaded_file($file['tmp_name'])) {


            $path = _PS_MODULE_DIR_ . 'custom/img/';
            $tmp_arr = explode('.', $file['name']);
            $filename = $file['name'];



            if (!Tools::copy($file['tmp_name'], $path . $filename)) {
                $errors[] = Tools::displayError('Failed to load image');
            }

        }
}

这里是 renderForm()

public function renderForm()
    {
      $image_url = '';

       if($this->object->file_url) {
            $image_url = ImageManager::thumbnail(
                _PS_MODULE_DIR_ . 'homecase/img/' . $this->object->file_url,
                $this->table . $this->object->file_url,
                150,
                'jpg',
                true,
                true
            );
        }
        $this->fields_form = [
            //Entête
            'legend' => [
                'title' => $this->module->l('Edition'),
                'icon' => 'icon-cog'
            ],

                array(
                    'type' => 'file',
                    'label' => $this->l('PC Image'),
                    'name' => 'file_url',
                    'display_image' => true,
                    'image' => $image_url ? $image_url : false,
                ),

....

上传和保存在数据库中是好的。但是当图像存在并且我没有在该字段中选择其他图像时。 file_url 字段在 DB 中被删除。

你能帮我吗?

谢谢!

【问题讨论】:

    标签: prestashop prestashop-1.7 prestashop-modules


    【解决方案1】:

    您需要检查图像是否已上传,然后才更新您的数据库记录。因此,只需尝试将 return false; 添加到您的 postImage 方法中,然后在您的数据库更新之前进行检查,例如

    if ($this->postImage($id) !== false){ //update image record }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-15
      • 2017-04-04
      • 1970-01-01
      • 2017-05-22
      • 2018-03-19
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多