2

I'm trying to validate a cakephp file upload

The following is the input in my view

<?php echo $this->Form->input('images.', array('type' => 'file', 'multiple', 'label'=>'Upload Images to your gallery')); ?>

This is the html code I get in the browser

<input type="file" required="required" id="ProjectImages" multiple="multiple" name="data[Project][images][]" />

The following is the code in my model for validation

'images[]' => array(
        'extension' => array(
            'rule' => array(
                'extension' => array('jpeg', 'png', 'jpg'),
                'message' => 'Please supply valid images'
            )
        ),
        'size' => array(
            'rule' => array('fileSize', '<=', '2MB'),
            'message' => 'Image must be less than 2MB'
        )
    )

I've also tried to validate with 'image' as the field name but both do not work. The files are uploading correctly but the validation is not working.

Please help. Thank you

1
  • The following is the code in my model for validation 'images[]' => array( - why did you think that would work? Are there any examples doing something similar? What code is reading/handling the images form data? You need to clearly demonstrate what you're doing - know that obviously a form validation rule designed for one value won't work when it's passed data in a different format. Commented Jun 15, 2014 at 11:53

3 Answers 3

1

Please check the thread linked here - Cakephp: Multiple files upload field sets to required automatically

I would suggest that you try to name the validation in the model as images rather than images[].If that still doesn't work, what I would suggest is for you to write a custom beforeSave function in your model and handle the validation yourself. You can use the following functions to do the validation.

public function isValidImageFile($filename) {
    if($this->checkFileUploadedName($filename) && 
        $this->checkFileUploadedLength($filename) && 
            $this->checkImgFileExtn($filename)) {
        return true;
    }
    return false;
}

private function checkFileUploadedName($filename)
{
    return (bool) ((preg_match("`^[-0-9A-Z_\.]+$`i",$filename)) ? true : false);
}

private function checkFileUploadedLength($filename)
{
    return (bool) ((mb_strlen($filename,"UTF-8") < 225) ? true : false);
}

private function checkImgFileExtn($filename) {
    $file_parts = pathinfo($filename);
    $supportedFileTypes =  array('jpg', 'png', 'jpeg', 'bmp');
    if(in_array(strtolower($file_parts['extension']), $supportedFileTypes)) {
        return true;
    }
    return false;
}

You could another method there to check the file size. You can check filesize using the php - filesize method.

Sign up to request clarification or add additional context in comments.

Comments

1

Try with name images instead of images[] and also correct for extension rule see Document Here

'images' => array(
        'extension' => array(
            'rule' => array(
                'extension',array('jpeg', 'png', 'jpg')
            ),
                'message' => 'Please supply valid images'
          ),
        'size' => array(
            'rule' => array('fileSize', '<=', '2MB'),
            'message' => 'Image must be less than 2MB'
        )
    )

Comments

0

Validation rules like mimeType or fileSize don't seem to work on multiple upload Arrays, you have to validate via custom rules:

$validator->add('uploads', [
    'fileSizeSum' => [
        'rule' => function($value, $context) {
            $fileSizeSumLimit = Configure::read('uploads')['max_uploadsize'];
            $fileSizeSum = 0;
            foreach($value as $key => $val) {
                $fileSizeSum += $val['size'];
                if( $fileSizeSum > $fileSizeSumLimit || $val['size'] > $fileSizeSumLimit ) {
                    return false;
                }
            }
            return true;
        },
        'on' => function ($context) {
            return ! empty($context['data']['uploads'][0]['size']);
        },
        'message' => __('Uploadlimit x MB!'),
    ],
    'extension' => [
        'rule' => function($value, $context) {
            $supportedFileTypes =  Configure::read('uploads')['allowed_mimetypes'];
            foreach($value as $key => $val) {
                $ext = pathinfo($val['name'], PATHINFO_EXTENSION);
                if( ! empty($ext) && ! in_array($ext, $supportedFileTypes) ) {
                    return false;
                }
            }
            return true;
        },
        'on' => function ($context) {
            return ! empty($context['data']['uploads'][0]['type']);
        },
        'message' => __('Allowed Mimetypes: '). implode(', ', Configure::read('uploads')['allowed_mimetypes_userrequest']),
    ]
]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.