Symfony HttpFoundation UploadedFile «не загружен из-за неизвестной ошибки» при использовании Doctrine DataFixtures
я использовал мою сущность вложения на основе получателя поваренной книгиКак обрабатывать загрузки файлов с помощью Doctrine в Symfony 2.3.
Хорошо работает даже в функциональных тестах. Однако использование его с Doctrine DataFixtures вызывает у меня проблемы.
[Symfony\Component\HttpFoundation\File\Exception\FileException]<br><br>The file "o-rly-copy.jpg" was not uploaded due to an unknown error.
Это не помогло, однако я бежалphp app/console doctrine:fixtures:load -v
чтобы вызвать трассировку стека, и кажется, что исключение выдается не на постоянный метод, а на$manager->flush()
Attachment::setFile()
требует экземпляраUploadedFile
так что мне интересно, есть ли способ обойти это.
Похоже, ошибка возникает в строке 225Symfony\Component\HttpFoundation\File\UploadedFile
return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname())
Условие дляis_uploaded_file()
возвращаетсяfalse
потому что файл уже был на сервере.
"O RLY?",
'file' => "o-rly",
'type' => "jpg",
),
//...
);
public function getPathToImages()
{
return $this->container->get('kernel')->getRootDir() . '/../src/Acme/DemoBundle/Resources/public/default/images';
}
public function getPathToUploads()
{
return $this->container->get('kernel')->getRootDir() . '/../web/uploads/fixtures';
}
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
$imageReferences = array();
$filesystem = $this->container->get('filesystem');
foreach (self::$imageData as $image) {
$imageFilename = sprintf('%s.%s', $image['file'], $image['type']);
$copiedImageFilename = sprintf('%s-copy.%s', $image['file'], $image['type']);
$pathToImageFile = sprintf('%s/%s', $this->getPathToImages(), $imageFilename);
try {
$filesystem->copy($pathToImageFile, $pathToCopiedFile = sprintf('%s/%s', $this->getPathToUploads(), $copiedImageFilename));
$filesystem->chmod($pathToCopiedFile, 0664);
} catch (IOException $e) {
$this->container->get('logger')->err("An error occurred while copying the file or changing permissions.");
}
$imageFile = new UploadedFile(
$pathToCopiedFile, // The full temporary path to the file
$copiedImageFilename, // The original file name
'image/' . 'jpg' === $image['type'] ? 'jpeg' : $image['type'], // Mime type - The type of the file as would be provided by PHP
filesize($pathToCopiedFile),
null,
null,
true
);
$imageAttachment = new Attachment();
$imageAttachment->setName($image['name']);
$imageAttachment->setFile($imageFile);
// Populate a reference array for later use
$imageReferences['attachment-'.$image['file']] = $imageAttachment;
$manager->persist($imageAttachment);
}
$manager->flush(); // $image) {
$this->addReference($referenceName, $image);
}
}
}