How to limit two maximum file sizes on one Magento site

So here is my problem. In the administration section of our Magento website, we need the ability to upload files from 2-500 MB each. I set my php.ini settings correctly, and all this is good with this requirement. But now I was asked to allow guests to download files from the external interface. Obviously, I do not want to give all strangers the opportunity to upload 500 MB of files. I searched around and could not find a decent direct answer to this problem.

So, how do you allow your administrator to continue to download extremely large files, while limiting third-party users to a smaller file size?

Here is my current solution:

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;
        }
    }
}

This checks if the file exceeds the limit. If so, it throws an exception.

, , , - / . .

+4
2

, Observer controller_action_predispatch POST , Mage_Core_Controller_Front_Action.

, , . , Core Magento.

.. /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>

/Observer.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;

                }
            }
        }
    }
}
+1

upload_max_filesize - PHP_INI_PERDIR. , php.ini,.htaccess, httpd.conf .user.ini( PHP 5.3). ini_set php . Apache, Nginx:

  • upload_max_filesize = 10M php.ini
  • Nginx location ~ \.php$ block

    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/is value from admin > routes > adminhtml > args > frontName magento_root/app/etc/local.xml

  • Nginx

+1

Source: https://habr.com/ru/post/1584385/


All Articles