doufuhao8085 2015-11-24 06:16
浏览 96

如何将变量传递给ZF2中的InputFilter :: add()方法

I don't understand why I can't pass any variable to the inputFilter->add(). In the apache logs the error just says:

[Mon Nov 23 23:35:35.690914 2015] [core:notice] [pid 1552] AH00052: child pid 3271 exit signal Segmentation fault (11)

And in the javascript console in chrome the error is:

status = (failed) net::ERR_INCOMPLETE_CHUNKED_ENCODING

The reason I want to do this is because I'm migrating the project from development to production a lot of times a day and its frustraiting to open this file and edit manually a line of code. I'm sure this could be automated by using the getcwd() function from PHP but I ignore how to acomplish this task.

This is my code

class MyClass implements InputFilterAwareInterface{
    public function getInputFilter(){
        if (!$this->inputFilter){
            $inputFilter = new InputFilter();
            $factory = new InputFactory();

            $inputFilter->add($factory->createInput(array(
                'name' => 'my-image', 
                'type' => 'Zend\InputFilter\FileInput',
                'required' => false,
                'allow_empty' => true,
                'priority' => 300,
                'filters' => array( 
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                    array(
                        'name' => 'Zend\Filter\File\RenameUpload',
                        'options' => array(
                            'target'    => getcwd() . "/media/img/imgUpload", // this line causes the error
                            'randomize' => true,
                            'use_upload_extension' => true,
                        )    
                    ),
                ), 
                'validators' => array(
                    array (
                        'name' => '\Zend\Validator\File\IsImage',
                    ),
                    array ( 
                        'name' => 'Zend\Validator\File\Size',
                        'options' => array(
                            'min' => '5kB', 
                            'max' => '10MB',
                        ), 
                    ),
                    array ( 
                        'name' => 'Zend\Validator\File\ImageSize',
                        'options' => array(
                            'minWidth' => 50,
                            'minHeight' => 50,
                            'maxWidth' => 2000,
                            'maxHeight' => 2000,
                        ), 
                    ),
                )))
            );
            $this->inputFilter = $inputFilter;
        }
        return $this->inputFilter;
    }
}

By the way, this code works well if I write the full path of my upload directory in my server in the field 'target' but as I mentioned above I'd like this 'target' to be server-independent.

Any ideas?

  • 写回答

2条回答 默认 最新

  • doudiza9154 2015-11-24 06:29
    关注

    Strange error. Hard to see exactly why it's being caused however you could try using the following magic constants to achieve the same result:

    PHP >= 5.3:

    'target'    => __DIR__ . "/media/img/imgUpload",
    

    or:

    PHP < 5.3:

    'target'    => dirname(__FILE__) . "/media/img/imgUpload",
    

    getcwd() is a really old PHP4 function, where as magic constants are a lot newer and more commonly used these days.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。