Tom*_* S. 5 validation symfony doctrine-extensions doctrine-uploadable
我需要将下载文件从 URL上传到我的服务器,并使用Uploadable (DoctrineExtensions)保留它。几乎一切都很好,我的方法是:
curl到我的服务器上的临时文件夹UploadedFile方法并用属性值填充它Media简化的代码:
// ... download file with curl
// Create UploadedFile object
$fileInfo = new File($tpath);
$file = new UploadedFile($tpath, basename($url), $fileInfo->getMimeType(), $fileInfo->getSize(), null);
// Insert file to Media entity
$media = new Media();
$media = $media->setFile($file);
$uploadableManager->markEntityToUpload($media, $file);
// Validate file (by annotations in entity)
$errors = $validator->validate($media);
// If no errors, persist and flush
if(empty($errors)) {
$em->persist($this->parentEntity);
$em->flush();
}
Run Code Online (Sandbox Code Playgroud)
如果我跳过验证,则一切正常。文件已成功移动到正确的路径(由 config.yml 中的可上传扩展配置)并保存到数据库。但手动创建的验证UploadedFile返回此错误:
文件无法上传。
我可以禁用 Validator 从 URL 上传并使用自定义方法验证文件,但使用 Symfony Validator 对象执行此操作对我来说似乎是一个更干净的解决方案。
有什么办法让它发挥作用吗?
小智 3
在 symfony 验证组件中,约束 UploadedFile 在内部使用此函数http://php.net/manual/es/function.is-uploaded-file.php
你可以在这里看到它https://github.com/symfony/HttpFoundation/blob/master/File/UploadedFile.php#L213
/**
* Returns whether the file was uploaded successfully.
*
* @return bool True if the file has been uploaded with HTTP and no error occurred.
*
* @api
*/
public function isValid()
{
$isOk = $this->error === UPLOAD_ERR_OK;
return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname());
}
Run Code Online (Sandbox Code Playgroud)
您必须创建自己的下载验证器(是的,您实际上是使用curl下载文件)
| 归档时间: |
|
| 查看次数: |
1259 次 |
| 最近记录: |