Jump to content

Product Customization - Upload ZIP file?


Recommended Posts

Hello everyone,

When adding a file upload option to a product's customization options it states that jpg, gif, and png are the available extensions. Is it possible to add a .zip extension to PrestaShop?

With a zip extension comes a larger size than what PrestaShop can currently handle. How can you modify the maximum file size to be uploaded? I have read other threads about modifying an htaccess file, but I was unable to find this.

Thank you!
Matt

Link to comment
Share on other sites

Hello Matt,

Sorry I've been very busy these past few days :-)

Okay heres what I've found so far...

#1. For obvious reasons change the text found on products.tpl (located in your active theme) around line 413 in regards to GIF, JPG and add ZIP.

#2. Open /yourstore/products.php and search for the function "pictureUpload" - its around line 13-14.

#3. Update this function to handle other uploads besides Images.

#4. This should take the user through the cart process fine, however there will need to be some updating in the back end when you are viewing the order so you can download he zip file instead of looking at a red x.

This is as far as I've gotten so far, sorry once again.

Link to comment
Share on other sites

Hi CY,

Thanks for the help so far, but I don't think I am that well versed in adding zip file types to the php. I have found the section, but it doesn't even list the current file types: jpg, gif, png.

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'))))
$errors[] = $error;
if (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') OR !move_uploaded_file($file['tmp_name'], $tmpName))
return false;
/* Original file */
elseif (!imageResize($tmpName, _PS_PROD_PIC_DIR_.$fileName))
$errors[] = Tools::displayError('An error occurred during the image upload.');
/* A smaller one */
elseif (!imageResize($tmpName, _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.');
elseif (!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.');
else
$cart->addPictureToProduct(intval($product->id), $indexes[$fieldName], $fileName);
unlink($tmpName);
}
return true;
}

On a side note, I have updated my php.ini file to handle larger upload sizes which is a start. :D

Thanks!
Matt

Link to comment
Share on other sites

I was also working on this issue.
I believe the function you want to modify is the CheckImage()
This function is in the images.inc.php around line 10-46
here you will find your extensions.

function    checkImage($file, $maxFileSize)
{
   if ($file['size'] > $maxFileSize)
       return Tools::displayError('image is too large').' ('.($file['size'] / 1000).Tools::displayError('KB').'). '.Tools::displayError('Maximum allowed:').' '.($maxFileSize / 1000).Tools::displayError('KB');
   if (!isPicture($file))
       return Tools::displayError('image format not recognized, allowed formats are: .gif, .jpg, .png');
   if ($file['error'])
       return Tools::displayError('error while uploading image; change your server\'s settings');
   return false;
}



the second condition checks to see the file type
This function looks at the extension


function isPicture($file)
{
   /* Detect mime content type */
   $mime_type = false;
   $types = array('image/gif', 'image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png');

   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'];

   // is it a picture ?
   return $mime_type && in_array($mime_type, $types);
}



You should be able to add onto the array list

   $types = array('image/gif', 'image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'file/zip');



You'll have to make other changes here and there, I'll post them when I get there.
My code might differ from yours, I had previously modified.

Link to comment
Share on other sites

Thanks for pointing me to this file!

So I got the zip extension to finally be read, but there is still a '1. An error occurred during the image upload.
' returned, and this language doesn't appear to be coming from the same file. Thoughts?

My form processing code gave me this:

POST:Array
(
   [textField26] => 
   [quantityBackup] => 
   [submitCustomizedDatas] => 1
)
FILES:Array
(
   [file25] => Array
       (
           [name] => testzip.zip
           [type] => application/x-zip
           [tmp_name] => C:\Windows\Temp\phpA561.tmp
           [error] => 0
           [size] => 40128
       )

)



To get the zip extension to be read, I modified my php.ini file to allow for large file uploads and large posts, and added these MIME types to line 76 in the array: 'application/zip', 'application/x-zip-compressed', 'application/x-zip'

I also found that $maxFileSize returns a smaller size than what I have defined in my php.ini file. Where is this variable defined?

I am going to keep messing with this, and will post a solution if I find one.

Link to comment
Share on other sites

I can't look at this until later, but I think the problem I am having is with PS trying to create a thumbnail of the ZIP with imageResize() which causes the upload to fail. I need to write a conditional that will allow for this function to be ignored when it is a ZIP extension.

Let me know if you solve this before I do.

Link to comment
Share on other sites

this is code from product.php/function pictureUpload(Product $product, Cart $cart)

if ($error = checkImage($file, intval(Configuration::get('PS_PRODUCT_PICTURE_MAX_SIZE'))))


Configuration - is table in PS Database.
PS_PRODUCT_PICTURE_MAX_SIZE is setted in the tab BO/Preferences/Product. Mine is 12000000 byte.


we are working about same problem. see other topics:

http://www.prestashop.com/forums/viewthread/27502/
http://www.prestashop.com/forums/viewthread/27570/

a suggest keep one topic only.

Link to comment
Share on other sites

  • 6 months later...
  • 5 months later...
  • 6 months later...
  • 2 years later...
  • 4 years later...
×
×
  • Create New...