Home is for the experience. Projects are for the ones who are interested!

How To Add New Extension To File Types In Prestashop

How To Add New Extension To File Types In Prestashop

This tutorial assume that you want to implement this functionality using your own code, not trying to upload using the prestashop file manager. So, in order to trick the file types extension in prestashop you need to implement an if statement in your file upload function so if the file is different from jpeg, gif or png, to not check the extension file. So your upload file function should look something like that:

public function makeUpload($file){
  if(!empty($file)){
    $success_files = array();
    $file_name = $file['name'];
    $rand_number = rand(0, 900);
    $temp = $file['tmp_name'];
    $file_object = array();
    $file_object['name'] = $file['name'];
    $file_object['type'] = $file['type'];
    $file_object['tmp_name'] = $file['tmp_name'];
    $file_object['error'] = $file['error'];
    $file_object['size'] = $file['size'];
    $ext = substr($file_name, strrpos($file_name, '.') + 1);
    $file_name = substr($file_name,0,strrpos($file_name,'.'));
    	$file_name = $file_name.'_'.$rand_number.'.'.$ext; 
    if (!file_exists(_PS_MODULE_DIR_.'modulename'.DIRECTORY_SEPARATOR.'files')) {
        mkdir(_PS_MODULE_DIR_.'modulename'.DIRECTORY_SEPARATOR.'files', 0777, true);
    }
    if (!move_uploaded_file($temp, _PS_MODULE_DIR_.'modulename'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR.$file_name)) {
      
      header('HTTP/1.1 500 Internal Server Error');
    		header('Content-type: text/plain');
      exit('An error occurred while attempting to upload the file.');
    }
  }
}

As you can see in the above function I don't validate my file, of course you can go deep in prestashop files and add your own extension but this way will take a lot of time. As an alternative method you can pass only the extension validation and keep the other 2.

Leave a Reply

Your email address will not be published. Required fields are marked *