How To Upload Images In Custom Prestashop Form

If you need to implement a custom form and upload images in prestashop you are in the right place because I will show you the entire code you will need to implement to achive this functionality. This tutorial is for prestashop 1.7 but you can try it can work for previews versions too. You don't need to much code because all functions are already implemente by prestashop, we will have to use them in order to upload images to a custom location and show it in frontend. 

First of all we will need to create a html for our form and it will look like the following.

<form id="uploadImage" action="pathtocontroller" enctype="multipart/form-data">
<input type="file" name="myfile">

<button type="submit" value="1" id="save_image" name="saveImage" class="btn btn-default pull-right">
    <i class="process-icon-save"></i> Save
</button>
</form>

This form will point to your custom controller where you will handle the data which will be received once the form is submitted. What is here very important is that you must to add the enctype parameter to the form, otherwise you will not receive notthing using $_FILES, once the parameter is added to your form you get the files.

Now we will move to controller file to postProcess function, if you don't have this function you have to create it, you can follow this tutorial if you don't know how to create the function. Our function should look like the following:

<?php
public function postProcess(){
  if(Tools::isSubmit('saveImage')){
    if(isset($_FILES)){
          
      $file_object = array();
      $file_object['name'] = $_FILES['name'];
      $file_object['type'] = $_FILES['type'];
      $file_object['tmp_name'] = $_FILES['tmp_name'];
      $file_object['error'] = $_FILES['error'];
      $file_object['size'] = $_FILES['size'];
      
      
      if ($error = ImageManager::validateUpload($file_object)) {
            	return $error;
        	} else {
        	$ext = substr($file_name, strrpos($file_name, '.') + 1);
            	$file_name = md5($file_name).'.'.$ext;
        if (!move_uploaded_file($temp, _PS_MODULE_DIR_.DIRECTORY_SEPARATOR.'mypath'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.$file_name)) {
                    return $this->displayError($this->trans('An error occurred while attempting to upload the file.', array(), 'Admin.Notifications.Error'));
                }else{
                	$myfile = _PS_BASE_URL_.'/modules/mymodule'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.$file_name;
                }
      }
    }				

  }				 
}

This is all code you need to create in order to upload image using a custom form in prestashop. Prestashop core features will check the image for you and if it return true then your image will be uploaded. You can add more parameters to the above function you can find all parameters like alowed file types, size and so on on the prestashop documentation website. This is what you need to do if you want to upload images in prestashop.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x