简体   繁体   English

如何验证Symfony2文件名?

[英]How can I validate Symfony2 filenames?

The following code will only validates the temporary filename (something like /tmp/phpsABCD) of an uploaded file 以下代码将仅验证上传文件的临时文件名(类似于/ tmp / phpsABCD)

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? 验证原始文件名的最佳方法是什么?

You can't. 你不能 PHP is server side. PHP是服务器端。 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. 您必须使用JavaScript在客户端上验证文件名,或使用将为您异步处理文件上传的JavaScript库。

Here you can play around: http://blueimp.github.io/jQuery-File-Upload/ 在这里您可以玩: http : //blueimp.github.io/jQuery-File-Upload/

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM