Matte01990 Posted November 17, 2016 Share Posted November 17, 2016 Hello everybody. I'm having some troubles in uploading an image and saving the path into db. This is my relevant code: <?php class AdminMyBlockContentController extends ModuleAdminController { public $bootstrap = true ; public function __construct() { $this->table = 'my_block_content'; $this->className = 'MyBlockContent'; $this->lang = true; $this->addRowAction('view'); $this->addRowAction('edit'); $this->addRowAction('delete'); //fields display $this-> fields_list = array( 'id_my_block_content' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25), 'nome' => array('title' => $this->l('Name'), 'width' => 120), 'desc' => array('title' => $this->l('Description'), 'width' => 120), 'link' => array('title' => $this->l('Link'), 'width' => 120), 'file_url' => array('title' => $this->l('file_url')), 'active' => array('title' => $this->l('Enabled'), 'align' => 'center', 'active' => 'status', 'type' => 'bool', 'orderby' => false, 'class' => 'fixed-width-xs') ); parent::__construct(); } //add form public function renderForm() { if (!($obj = $this->loadObject(true))) return; $this->fields_form = array( 'legend' => array( 'title' => $this->l('Add/Edit Box') ), 'input' => array( array( 'type' => 'text', 'label' => 'Nome', 'name' => 'nome' ), array( 'type' => 'textarea', 'label' => 'Description', 'name' => 'desc', 'autoload_rte' => 'rte' //Enable TinyMCE editor for short description ), array( 'type' => 'file', 'label' => $this->l('file_url'), 'name' => 'file_url', 'display_image' => true ), array( 'type' => 'text', 'label' => 'Link', 'name' => 'link' ), array( 'type' => 'switch', 'label' => $this->l('Enable'), 'name' => 'active', 'required' => false, 'class' => 't', 'is_bool' => true, 'values' => array( array( 'id' => 'active_on', 'value' => 1, 'label' => $this->l('Enabled') ), array( 'id' => 'active_off', 'value' => 0, 'label' => $this->l('Disabled') ) ) ) ), 'submit' => array( 'title' => $this->l('Save'), 'name' => 'submit_form' ) ); return parent::renderForm(); } public function postProcess() { //checks if submit if (Tools::isSubmit('submit_form')) { //insert data into my_block_content $insertData = array( 'nome' => Tools::getValue('nome'), 'desc' => Tools::getValue('desc'), 'active' => Tools::getValue('active'), 'link' => Tools::getValue('link'), 'file_url' => Tools::getValue('file_url') ); Db::getInstance()->insert("my_block_content", $insertData); } //checks if delete elseif (Tools::isSubmit('delete').$this->table) { Db::getInstance()->delete('my_block_content', 'id_my_block_content = '.$_GET[id_my_block_content]); } } } Others fields are saved into the right table, so there are no other issues. Any help would be appreciated! Thank you ! Link to comment Share on other sites More sharing options...
Matte01990 Posted November 17, 2016 Author Share Posted November 17, 2016 Thank you for answering! Where should I add your code snippet? Into postProcess() function? And do I need to specify the exact directory path in order to upload the image? If so, how? Thank you! Link to comment Share on other sites More sharing options...
Matte01990 Posted November 18, 2016 Author Share Posted November 18, 2016 Solved: //INSERT if (Tools::isSubmit('submit_form')) { //file upload code if (isset($_FILES['file_url'])) { $target_dir = _PS_UPLOAD_DIR_; $target_file = $target_dir . basename($_FILES['file_url']["name"]); $uploadOk = 1; $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES['file_url']["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } // Check if file already exists if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; } else { if (move_uploaded_file($_FILES['file_url']["tmp_name"], $target_file)) { echo "The file ". basename($_FILES['file_url']["name"]). " has been uploaded."; $file_location = basename($_FILES['file_url']["name"]); } else { echo "Sorry, there was an error uploading your file."; } } } 1 Link to comment Share on other sites More sharing options...
jborse Posted March 15, 2017 Share Posted March 15, 2017 Can please share your module Matte01990 Link to comment Share on other sites More sharing options...
Matte01990 Posted March 21, 2017 Author Share Posted March 21, 2017 Sorry mate. I have no access to that project anymore. Btw that was the relevant code Link to comment Share on other sites More sharing options...
Shara_AC Posted August 16, 2019 Share Posted August 16, 2019 I need help. when I want to make a preview image on the customer page register. how to do it for version 1.7.5.1 Link to comment Share on other sites More sharing options...
MSLT Posted August 17, 2020 Share Posted August 17, 2020 Old topic but whatever, maybe someone finds this useful Add this to your input field onchange="readURL(this);" And use javascript to show it function readURL(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function (e) { $('.icon-img') // dom where you put preview img .attr('src', e.target.result) }; reader.readAsDataURL(input.files[0]); } } Link to comment Share on other sites More sharing options...
Saif Ali Ansari Posted February 5, 2022 Share Posted February 5, 2022 (edited) Hello , How did you register the controller in the main file of the project without creating the tab. Now I am working on the upload a pdf file on the product page using displayAdminProductsExtra tab. Please share your opinion. I am stuck. thanks... Edited February 5, 2022 by Saif Ali Ansari (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now