Jump to content

Image upload on custom modules


Matte01990

Recommended Posts

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

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.";
					}
				}
				
			}
  • Like 1
Link to comment
Share on other sites

  • 3 months later...
  • 2 years later...
  • 1 year later...

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

  • 1 year later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...