How can I validate Symfony2 filenames?

1k views Asked by At

The following code will only validates the temporary filename (something like /tmp/phpsABCD) of an uploaded file

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Assert\NotBlank()
 * @Assert\Regex(
 *     pattern="/^[a-zA-Z0-9\/._-]+$/",
 *     message="Invalid filename"
 * )
 * @var UploadedFile
 */
private $file;

But I want to avoid that users upload files with umlauts (äü), brackets and similar characters. What is the best way to validate original filenames?

1

There are 1 answers

0
createproblem On

You can't. PHP is server side. You have to validate the filename on the client side by using JavaScript, or use a JavaScript library that will handle file uploads asynchronously for you.

Here you can play around: http://blueimp.github.io/jQuery-File-Upload/

Cheers.