1

i an trying to upload a file and i need it to contain only letters, numbers and _

here is my code

    $regex = new Zend_Validate_Regex('/^[a-zA-Z0-9_.]+$/i');
    $regex->setMessages(array(
        Zend_Validate_Regex::NOT_MATCH => 'NOT MATCH - File name can only contain letters, numbers and underscores',
        Zend_Validate_Regex::INVALID   => 'INVALID - File name can only contain letters, numbers and underscores',
        Zend_Validate_Regex::ERROROUS  => 'ERROR - File name can only contain letters, numbers and underscores'
    ));

    $file = new Zend_Form_Element_File('file');
    $file->setRequired(TRUE)
        ->setDestination($path)
        ->addValidator('Size', FALSE, array('min' => '10kB', 'max' => '100MB'))
        ->addValidator('Extension', FALSE, 'avi,mov,wmv,mpeg,flv,mpg,mp4')
        ->addValidator($regex)
        ->getValidator('Upload')->setMessage('No file selected');
    $file->removeDecorator('DtDdWrapper');
    $file->removeDecorator('HtmlTag');
    $file->getDecorator('Label')->setTag(NULL);

i want this file to pass: test_test.jpeg

but not this one: test test.jpeg

the issue i am having is that even if the file name is OK the validator still throws the NOT_MATCH error

i even allowed . in case the file name is the entire file name string

any ideas?

2
  • 1
    Well maybe the filename isn't stored as 'test_test.jpg' but as 'c:\fakepath\test_test.jpg'? Commented Feb 13, 2014 at 23:33
  • good point, that seems to be the issue, but i still need to validate the file name only. not sure how to do that Commented Feb 13, 2014 at 23:41

1 Answer 1

1
/^(.*/)?[a-zA-Z0-9_.]*?\.[a-zA-Z0-9_]*?$/i

Get rid of everything past the "\." if you don't require an extension. Otherwise, this should work for both full path and file-only names

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

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.