【问题标题】:How to have two max file size limits on one Magento site如何在一个 Magento 站点上设置两个最大文件大小限制
【发布时间】:2015-04-24 01:43:25
【问题描述】:

所以这是我的问题。在我们 Magento 站点的管理部分,我们需要能够上传文件,每个文件可以是 2-500 MB 的任何地方。我已经适当地设置了我的 php.ini 设置,并且一切都很好地满足了这个要求。但现在我被要求允许客人从前端上传文件。显然,我不想让完全陌生的人能够上传 500 MB 的文件。我已经四处搜索,但无法找到一个体面的直截了当的答案来解决这个问题。

那么,您如何让您的管理员继续上传超大文件,同时将前端用户限制为较小的文件?

这是我目前的解决方案:

public function saveAction()
{
    $post = $this->getRequest()->getPost();
    $helper = Mage::helper('my_module');
    if ( $post ) {
        try {
            if ($_FILES['size'] >= 2000000) { // Limit is set to 2 MB
                $errors[] = $helper->__('You have exceeded the max file size.');
                $error = true;
            }
            if ($error) {
                throw new Exception();
            }
            // Perform save operations here.
        } catch (Exception $e) {
            foreach($errors as $error) {
                Mage::getSingleton('core/session')->addError($error);
            }
            $this->_redirect('*/*/*');
            return;
        }
    }
}

这会检查文件是否超出限制。如果是,则抛出异常。

我意识到这个解决方案很简单,这就是为什么我四处询问是否有人有更好/替代的解决方案。我期待着阅读您的答案。

【问题讨论】:

    标签: php magento file-upload filesize


    【解决方案1】:

    您可以做的是向事件 controller_action_predispatch 添加一个观察者,然后从那里只捕获 POST 发送到扩展 Mage_Core_Controller_Front_Action 的控制器。

    这样,您将在任何操作中发布每个文件,而不必一次又一次地重做相同的工作。额外的好处是,当使用观察者时,你不会弄乱 Magento 的核心。

    etc/config.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <Bdotenoitdotbe_Module>
                <version>0.0.0.1</version>
            </Bdotenoitdotbe_Module>
        </modules>
        <global>
            <models>
                <bdotenoitdotbe_module>
                    <class>Bdotenoitdotbe_Module_Model</class>
                </bdotenoitdotbe_module>
            </models>
        </global>
        <frontend>
            <events>
                <controller_action_predispatch>
                    <observers>
                        <bdotenoitdotbe_module_controller_action_predispatch>
                            <class>bdotenoitdotbe_module/observer</class>
                            <method>parseFiles</method>
                        </bdotenoitdotbe_module_controller_action_predispatch>
                    </observers>
                </controller_action_predispatch>
            </events>
        </frontend>
    </config>
    

    模型/观察者.php

    <?php
    class Bdotenoitdotbe_Module_Model_Observer {
        const MAX_FRONTEND_UPLOAD_SIZE = 2000000;
    
        public function parseFiles($observer){
            if($observer->getEvent()->getControllerAction() instanceof Mage_Core_Controller_Front_Action &&
                $observer->getEvent()->getControllerAction()->getRequest()->isPost()) {
    
                foreach($_FILES as $file_key => $file) {
                    if($file['size'] > self::MAX_FRONTEND_UPLOAD_SIZE) {
                        Mage::getSingleton('core/session')->addError('File too big : '.$file['name']);
                        /**
                         * you can do unset($_FILES[$file_key]);
                         * but I would rather do the following to simulate a file too big behaviour
                         */
    
                        $file['error'] = UPLOAD_ERR_FORM_SIZE;
                        $file['tmp_name'] = null;
                        $file['size'] = 0;
                        $_FILES[$file_key] = $file;
    
                    }
                }
            }
        }
    }
    
     
    

    【讨论】:

    • 另一个有趣的答案。我喜欢创建自己的观察者的想法,该观察者可以普遍处理您所连接的模块的所有文件帖子。我会试一试,然后告诉你进展如何。
    【解决方案2】:

    upload_max_filesize 是 PHP_INI_PERDIR 条目。这意味着这个值可以在 php.ini、.htaccess、httpd.conf 或 .user.ini 中设置(自 PHP 5.3 起)。您不能为此在 php 文件中使用 ini_set。 不确定 Apache,但这里是 Nginx 的操作方法:

    1. 在 php.ini 中设置 upload_max_filesize=10M
    2. location ~ \.php$ 块内的 Nginx 配置中添加下一行

      if ($request_uri ~ /admin/){
          client_max_body_size 500m;
      }
      

      if ($request_uri ~ /admin/) {
          fastcgi_param PHP_VALUE "upload_max_filesize = 500M \n post_max_size=500M";
      }
      

      注意:/admin/ 是来自 admin > routes > adminhtml > args > frontName in magento_root/app/etc/local.xml 的值

    3. 重启 Nginx

    【讨论】:

    • 这很有趣。我没有意识到您可以为upload_max_filesize 设置特定的目录。我将尝试一下,看看它是如何工作的。
    猜你喜欢
    • 1970-01-01
    • 2015-10-21
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 1970-01-01
    • 2020-12-22
    相关资源
    最近更新 更多