Jump to content

[SOLVED]How customers could upload their own ZIP files ?


Recommended Posts

Hi All !

I would like to propose my customers to upload their own ZIP files from the product customization fields in front office.
Pieces of solutions occurs in both french and english forums.
Is there someone who could explain step by step how to proceed for that ?
It would be appreciable to solve definitively this point : It would be nice to put [sOLVE] to this post.

Thanks a lot !

Link to comment
Share on other sites

1. Go to your back office.
2. Create a new product and then there will be "Customization" tab
3. Enter number to "file fields".
4. Then click to "update settings"
5. After that you will see blank, there you can write upload detail.
6. Then click to "save" button.
7. Finally, go to your front office, click to product, there will see "Product customization" tab.

I hope it will be helpfull to you.

21001_BLR4H4WtTQz7HTRriWD4_t

21002_svOq7mcneYUQRCgnmOqS_t

Link to comment
Share on other sites

Hi again,

Someone told me : Help yourself and heaven will help you ! So, here is the solution :

1/ Modify product.php as below :

function isZIP($file)
{
   /* Detect mime content type */
   $mime_type = false;
   $types = array('multipart/x-zip','application/zip','application/x-zip-compressed','application/x-zip');

   if (function_exists('finfo_open'))
   {
       $finfo = finfo_open(FILEINFO_MIME);
       $mime_type = finfo_file($finfo, $file['tmp_name']);
       finfo_close($finfo);
   }
   elseif (function_exists('mime_content_type'))
       $mime_type = mime_content_type($file['tmp_name']);
   elseif (function_exists('exec'))
       $mime_type = trim(exec('file -b --mime-type '.escapeshellarg($file['tmp_name'])));
       if (empty($mime_type) || $mime_type == 'regular file')
               $mime_type = $file['type'];
       if (($pos = strpos($mime_type, ';')) !== false)
               $mime_type = substr($mime_type, 0, $pos);
   // is it a picture ?

   return $mime_type && in_array($mime_type, $types);
}

function pictureUpload(Product $product, Cart $cart)
{
   global $errors;

   if (!$fieldIds = $product->getCustomizationFieldIds())
       return false;
   $authorizedFileFields = array();
   foreach ($fieldIds AS $fieldId)
       if ($fieldId['type'] == _CUSTOMIZE_FILE_)
           $authorizedFileFields[intval($fieldId['id_customization_field'])] = 'file'.intval($fieldId['id_customization_field']);
   $indexes = array_flip($authorizedFileFields); 
   foreach ($_FILES AS $fieldName => $file)
       if (in_array($fieldName, $authorizedFileFields) AND isset($file['tmp_name']) AND !empty($file['tmp_name']))
       {
           $fileName = md5(uniqid(rand(), true));
           if (($error = checkImage($file, intval(Configuration::get('PS_PRODUCT_PICTURE_MAX_SIZE')))) && (!isZIP($file))) {
               $errors[] = $error;
               return false;
           }
           if (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') OR !move_uploaded_file($file['tmp_name'], $tmpName)) {
               return false;
           }

           if (isZIP($file)) {
                   $fileName = "P" . $fileName;
                   if (!copy($tmpName, _PS_PROD_PIC_DIR_.$fileName)) {
                                $errors[] = Tools::displayError('An error occurred during the file upload.');
                       return false;
                   }
                   $oldfile = './img/zip.jpg';
           } else {
                   $fileName = "J" . $fileName;
                   if (!imageResize($tmpName, _PS_PROD_PIC_DIR_.$fileName)) {
                                $errors[] = Tools::displayError('An error occurred during the image upload.');
                       return false;
                   }
                   $oldfile = $tmpName;
           }
           if (!imageResize($oldfile,  _PS_PROD_PIC_DIR_.$fileName.'_small', intval(Configuration::get('PS_PRODUCT_PICTURE_WIDTH')), intval(Configuration::get('PS_PRODUCT_PICTURE_HEIGHT')))) {
                        $errors[] = Tools::displayError('An error occurred during the image upload.');
               return false;
           }                    

                       if (!chmod(_PS_PROD_PIC_DIR_.$fileName, 0777) OR !chmod(_PS_PROD_PIC_DIR_.$fileName.'_small', 0777)) {
                               $errors[] = Tools::displayError('An error occurred during the image upload.');
               return false;
           }

           $cart->addPictureToProduct(intval($product->id), $indexes[$fieldName], $fileName);

           unlink($tmpName);
       }
   return true;
}



2/ Modify displayImage.php as follow :

<?php

$name = $_GET['name'];
$file = $_GET['img'];

require_once(dirname(__FILE__).'/../config/config.inc.php');
require_once(dirname(__FILE__).'/init.php');

$type = substr($file, 0, 1);
$img = substr($file, 1);

if ($img AND Validate::isMd5($img) AND $name AND Validate::isGenericName($name) AND file_exists(_PS_PROD_PIC_DIR_.$file))
{

   if ($type == "P") {
       header('Content-type: application/zip'); /*'multipart/x-zip','application/zip','application/x-zip-compressed','application/x-zip'*/
       $ext = ".zip";    
   } elseif ($type == "J") {
       header('Content-type: image/jpeg');
       $ext = ".jpg";
   }

   header('Content-Disposition: attachment; filename="'.$name.$ext.'"');
   echo file_get_contents(_PS_PROD_PIC_DIR_.$file);
}

?>



3/ Add ZIP icon (zip.jpg) into yourwebsite/img

Bye.

21110_gTdmF3ADPbT12CNiz5Mo_t

Link to comment
Share on other sites

  • 1 month later...
  • 5 weeks later...
  • 2 weeks later...
  • 2 months later...
  • 6 months later...
  • 4 months later...
  • 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...