gpt4 book ai didi

使用 Doctrine DataFixtures 时的 Symfony HttpFoundation UploadedFile "not uploaded due to unknown error"

转载 作者:行者123 更新时间:2023-12-02 17:12:27 25 4
gpt4 key购买 nike

我一直在根据食谱使用我的附件实体 How To Handle File Uploads With Doctrine在 Symfony 2.3 中。

即使在功能测试中,它也运行良好。然而,将它与 Doctrine DataFixtures 一起使用会给我带来问题。

[Symfony\Component\HttpFoundation\File\Exception\FileException]<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 的实例所以我想知道是否有办法解决这个问题。

看来错误发生在 Symfony\Component\HttpFoundation\File\UploadedFile 的第 225 行

return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname())

is_uploaded_file() 的条件返回false因为该文件已经在服务器上。

<?php

/**
* Prepopulate the database with image attachments.
*/
final class AttachmentFixtures extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
private static $imageData = array(
array(
'name' => "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(); // <-- Exception throw here


// Create references for each image to be used by other entities that
// maintain a relationship with that image.
foreach ($imageReferences as $referenceName => $image) {
$this->addReference($referenceName, $image);
}
}
}

最佳答案

现在有更好的解决方案:

UploadedFile 的构造函数有一个 bool 值 $test 参数,该参数禁用使用 is_uploaded_file 进行检查。此参数已添加用于测试/夹具代码。

只需将其设置为 true,UploadedFileisValid() 检查将不再是问题。

示例:

// My data fixture code.
$test = true;
$userPhoto->setImageFile(new UploadedFile($photoDir . $photoFile, $photoFile, null, null, null, $test));

关于使用 Doctrine DataFixtures 时的 Symfony HttpFoundation UploadedFile "not uploaded due to unknown error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18978295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com