yakamoko
yakamoko

Reputation: 25

how to pass a variable to the InputFilter::add() method in ZF2

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?

Upvotes: 1

Views: 95

Answers (2)

yakamoko
yakamoko

Reputation: 25

After a couple of hours making experiments I discovered it was my mistake: I missed to include the 'public' directory in the path, so the final working code that solved my problem is:

$this->rutaUploadImagenes = getcwd() . "/public/media/img/imgUpload"; // this line is located in the __construct()
'target' => $this->rutaUploadImagenes,

Upvotes: 0

Maltronic
Maltronic

Reputation: 1802

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.

Upvotes: 1

Related Questions