Hi,
I have quite a problem here. So I'm developing a module and one of it's features is to create a project which contains a file (image).
While creating it and saving this image is working fine, when you try to edit it then the problem appears. If you edit some fields and leave the file input untouched Prestashop overrides file with empty field.
I already checked that inside fields_value the files name is correct and I can't find the moment where it's getting overridden. Also tried using HelperForm() and setting $this->tpl_vars or $this->object->file. Nothing worked, everytime form gets submitted with file input empty unless you submit anothe file.
Here is form rendering function:
public function renderForm() { $this->fields_form = array( 'legend' => array( 'title' => $this->trans('New project', array(), 'Admin.Catalog.Feature'), ), 'input' => array( array( 'type' => 'text', 'label' => $this->trans('Name', array(), 'Admin.Global'), 'name' => 'name', 'required' => true, 'col' => 3, ), array( 'type' => 'select', 'options' => array( 'query' => Project::getGroups(), 'id' => 'id_group', 'name' => 'name', ), 'label' => $this->trans('Group', array(), 'Admin.Global'), 'name' => 'id_group', 'required' => true, 'col' => 2, ), array( 'type' => 'select', 'options' => array( 'query' => Project::getProducts((int)Context::getContext()->language->id), 'id' => 'id_product', 'name' => 'name', ), 'label' => $this->trans('Product', array(), 'Admin.Global'), 'name' => 'id_product', 'class' => 'js-example-basic-single', 'required' => true, 'col' => 3, ), array( 'type' => 'text', 'label' => $this->trans('Height', array(), 'Admin.Global'), 'name' => 'height', 'required' => true, 'col' => 1, ), array( 'type' => 'text', 'label' => $this->trans('Width', array(), 'Admin.Global'), 'name' => 'width', 'required' => true, 'col' => 1, ), array( 'type' => 'file', 'label' => $this->trans('File', array(), 'Admin.Global'), 'name' => 'file', 'required' => false, 'col' => 6, ), array( 'type' => 'text', 'label' => $this->trans('CSS Class', array(), 'Admin.Global'), 'name' => 'class_name', 'required' => false, 'col' => 3, ), ), 'submit' => array( 'title' => $this->trans('Save', array(), 'Admin.Actions'), 'name' => 'save_form', ), ); return parent::renderForm(); }
Here is file uploading function:
public function postProcess() { parent::postProcess(); if (Tools::getIsset('save_form')) { // Uploads project image file if ($_FILES['file']['name'] != '') { $project = new Project(Project::getNewestProjectId()); $project->file = $project->uploadFile($_FILES['file'], $project->id_project); $project->save(); } $this->redirect_after = Context::getContext()->link-> getAdminLink('AdminPersonalizationDesign') . '&id_project=' .Project::getCurrentProjectId(); } } } public function uploadFile($file, $id_project) { $fileName = explode(".", $file["name"]); $ext = end($fileName); $name = uniqid(reset($fileName)).".".$ext; $id_folder = (int)ceil($id_project/1000); if (!file_exists(_PS_IMG_DIR_ ."pp/projects/".$id_folder)) { mkdir(_PS_IMG_DIR_ ."pp/projects/".$id_folder); } $location = _PS_IMG_DIR_ ."pp/projects/".$id_folder."/". $name; move_uploaded_file($file["tmp_name"], $location); return $name; }