0

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

在此处输入图像描述

如果在这种情况下单击“保存”,则会删除现场 PC 图像。

这是我的postImage()方法AdminCustomController

 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字段在数据库中被删除。

你可以帮帮我吗?

谢谢 !

4

1 回答 1

0

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

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

于 2020-02-21T09:51:43.423 回答