Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8970629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:45:21+00:00 2026-06-15T17:45:21+00:00

Actually I’m developing a simple file uploader. In the ImageUploader.php file I define the

  • 0

Actually I’m developing a simple file uploader.

In the ImageUploader.php file I define the getInputFilter function, all works unless I try to add a File\MimeType validator:

<?php

namespace Admin\Model;

use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

use Zend\Validator\File\MimeType; //tried also with use Zend\Validator\File;

[...]
public function getInputFilter()
{
    [...]
    $inputFilter->add($factory->createInput(array(
       'name'     => 'image',
       'required' => true,
       'validators' => array(
            array(
                'name'    => 'MimeType', //tried also with File\MimeType
                'options' => array(
                   'mimeType' => array('image/jpeg'),
                ),
            ),
        ),
    )));
    [...]
}

What’s the correct way to define a File\Validator\MimeType?

Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:45:22+00:00Added an answer on June 15, 2026 at 5:45 pm

    Here is a sample from my project with a couple of other useful features (renaming for example):

    use Zend\File\Transfer\Adapter\Http,
        Zend\Validator\File\Size,
        Zend\Validator\File\IsImage;
    
    [...]
    public function editAction() {    
    [...]
      if ($request->isPost()) {
        $params = $this->params()->fromPost();
        $files = $this->params()->fromFiles();
    
        $images = array();
        if(!empty($files['main_image']['name'])) $images['main_image'] = $files['main_image']['name'];
        if(!empty($files['detailed_image']['name'])) $images['detailed_image'] = $files['detailed_image']['name'];
    
        if(!empty($images)) {
          $adapter = new Http();
          $size = new Size(array('max'=>1000000));
          $is_image = new IsImage();
          $image_errors = array();
          foreach ($images as $field => $filename) {
            $adapter->setValidators(array($size,$is_image), $filename);
            if (!$adapter->isValid($filename)) {
              $adapter_errors = $adapter->getMessages();
              $errors = array();
              foreach($adapter_errors as $key=>$row) {
                $errors[] = $row;
              }
              if(!empty($errors)) {
                array_push($image_errors, $errors);
                $form->setMessages(array($field => $errors));
              }
            } else {
              $adapter->setDestination(IMAGES_PATH);
              $fileinfo = $adapter->getFileInfo();
              preg_match('/.+\/(.+)/', $fileinfo[$field]['type'], $matches);
              $extension = $matches[1];
              $old_filename = $item->__get($field);
              $new_filename = $item->id.'_'.$field.'.'.$extension;
    
              $adapter->addFilter('File\Rename',
                array(
                  'target' => IMAGES_PATH.$new_filename,
                  'overwrite' => true,
                )
              );
    
              if($adapter->receive($fileinfo[$field]['name'])) {
                if(!empty($old_filename) && $old_filename != $new_filename &&
                  file_exists(IMAGES_PATH.$old_filename)) {
                  unlink(IMAGES_PATH.$old_filename);
                }
                $params[$field] = $new_filename;
              }
            } 
          }
        }
      }
    [...]
    }
    [...]
    

    The code however could be refactored to move the logic to some lib or model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Actually I am developing one application in that when application run first time it
Actually ,i am working on a livechat application its works fine.This application is tabbed
actually, i'm developing a web template using ASP.NET and C#. i have a listview
Actually I've (probably) a simple problem. So I don't know how to cast a
Actually I'm working on simple project MVC4 + EF in which I have relation
Actually it works if i use port 8080 or some values bigger than 1023(i
Actually I don't know how to define this idioms. In some code i have
Actually I was learning PHP, so this question just came into my mind. How
Actually i am developing a Map browser app in Android. I have a .dat
actually I'm have a very simple problem (that's for sure) I just can't find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.