2

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?

Xover
  • 588
  • 5
  • 10
  • maybe have a look at `$this->getFile()->getClientOriginalName()` from http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html – Toskan Apr 10 '14 at 21:15

1 Answers1

0

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.

createproblem
  • 1,542
  • 16
  • 30