Max file size for Magento custom option uploads?
I've l开发者_JAVA技巧ooked everywhere on Google but I cannot see if there's a way to do this - I have custom options on my Magento products which allow the customer to upload a file. But I need to put a filesize limit on this to stop someone sending me a 99GB file which brings down the server.
There must surely be somewhere I can set this?
This is related to php setting upload_max_filesize
. You can change this from php.ini and other variables affected are post_max_size
(the amount of data that can be posted via forms) and also everything sent must fit to memory/disk
see more from php manual
You can change the max size attribute via .htaccess
, this is true. But you could be more fine-grained inside Magento via a callback function with Varien_File_Uploader
for a given interface. So you could set the size of an application to have a post value 50MB
as a maximum site wide with a .htaccess
file but restrict the file limits on certain interfaces further, 10MB
for A 20MB
for B, and so on...
Reusing the field image_size_y in the table catalog_product_option, we can set the max file size here. Then, we need to overwrite 2 core files and 2 phtml files.
1. Overwrite Mage_Catalog_Model_Product_Option
protected function _afterLoad()
{
$this->setMaxFileSize();
return parent::_afterLoad();
}
public function setMaxFileSize()
{
if (!preg_match('/(jpg|png|bmp)/i', $this->getFileExtension())) {
$this->setData('max_file_size', $this->getImageSizeY());
$this->setData('image_size_y', 0);
} else {
$this->setData('max_file_size', 0);
}
return $this;
}
2. Overwrite Mage_Catalog_Model_Product_Option_Type_File
public function setOption($option)
{
$option->setMaxFileSize();
$this->_option = $option;
return $this;
}
protected function _getUploadMaxFilesize()
{
if ($kb = $this->getOption()->getMaxFileSize()) {
return min(parent::_getUploadMaxFilesize(), $kb*1024);
}
return parent::_getUploadMaxFilesize();
}
protected function _bytesToMbytes($bytes)
{
return round($bytes / (1024 * 1024), 2);
}
3. \app\design\frontend\yourpackage\default\template\catalog\product\view\options\type\file.phtml:
<?php $_option = $this->getOption()->setMaxFileSize(); // replace first line of code ?>
...
<!-- add the following after <?php if ($_option->getImageSizeY() > 0): ?>...<?php endif; ?> -->
<?php if ($_option->getMaxFileSize() > 0): ?>
<script type="text/javascript">
$('options_<?php echo $_option->getId() ?>_file').addClassName('validate-file-size');
$('options_<?php echo $_option->getId() ?>_file').setAttribute('max-file-size', <?php echo $_option->getMaxFileSize() ?>);
</script>
<p class="no-margin"><?php echo Mage::helper('catalog')->__('Maximum file size')?>: <strong><?php echo $_option->getMaxFileSize() ?> <?php echo Mage::helper('catalog')->__('KB')?></strong></p>
<?php endif; ?>
4.\app\design\frontend\yourpackage\default\template\catalog\product\view\options.phtml, add the following js:
Validation.add('validate-file-size', '<?php echo $this->jsQuoteEscape(Mage::helper('catalog')->__('Exceeded maximum file size'))?>',
function(value, elm) {
var maxsize = +elm.getAttribute('max-file-size') || 1024; // kb
var size = elm.files[0].size/1024; // kb
return maxsize > size;
}
);
The above tested in Magento 1.9.0.1 on Firefox, the validation script may not work in IE.
精彩评论