我需要将下载文件从 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 中的可上传扩展配置)并保存到数据库。但手动创建的验证 …