- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在根据食谱使用我的附件实体 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,UploadedFile
的 isValid()
检查将不再是问题。
示例:
// 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/
我目前正在开发一个应用程序,用户可以选择浏览和上传 Excel 文件,但我很难获取正在浏览的文件的绝对路径。因为位置可以是任何东西(Windows/Linux)。 import org.apache.
在 Ubuntu 服务器上部署我的 Laravel 代码后,我有以下函数返回空字符串: $package = $request->file('file'); dd($package->getPathn
这个问题在这里已经有了答案: How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile
我阅读了以下 django 文档 UploadedFile.content_type The content-type header uploaded with thefile (e.g. text/
我正在寻找一个 javascript/jquery(无论哪种方式)来收集我上传的所有文件。 我有以下代码,其中 .afbeelding 是几个文件输入字段的类 var geuploadeAfbeeld
我有一个 Laravel 4.2 API,它在创建资源时接受文件上传。该文件检索与Input::file('文件') 现在我想编写一个脚本(也在 Laravel 中)来批量创建一些资源(所以我不能使用
我正在尝试从现有文件创建一个 UploadedFile。但它没有成功上传,也没有显示任何错误。 这是我的代码 $encoded_data = "This is a huge string";
我正在使用 上传文件(图片)在代码隐藏中我使用 UploadedFile.SaveAs("C://Path...")将图像保存在服务器上。 这是我的完整代码: protected void btnA
当我的表单被发送时 UploadedFile::getInstance($model, 'images') 返回 null。我还尝试了 UploadedFile::getInstanceByName(
我在 symfony2 中使用 UploadedFile 类来上传图像文件。我允许使用 mime 类型 image/jpg 和 image/png 的图像。当我将 png 文件从我的系统上传到服务器时
我正在使用 PrimeFaces 3.2,我应该在文件系统上存储一个文件。 现在,我知道 FileUploaded 类创建了一个临时文件,因此我可以使用 java.io.File 将其转换为真实文件。
我有以下函数定义: public function save(UploadedFile $file, string $fileSystemName) { $fs = $this->fileSy
我正在构建文件上传没有 Doctrine 并且没有通过我可用的注释或 YAML 进行验证。 Symfony 将上传的文件呈现为 Symfony\Component\HttpFoundation\Fil
我正在构建文件上传没有 Doctrine 并且没有通过我可用的注释或 YAML 进行验证。 Symfony 将上传的文件呈现为 Symfony\Component\HttpFoundation\Fil
我有一个包含 CSV 数据的巨大字符串。我想将其转换为 Excel 文件 (.xslx) 并将其另存为 UploadedFile/SimpleUploadedFile。我尽可能地用谷歌搜索并得出以下结
我正在学习本教程 http://symfony.com/doc/current/controller/upload_file.html但是 $file 没有被转换为 UploadedFile,它仍然是
PrimeFaces UploadedFile 仅公开 InputStream,不公开文件本身。处理流后如何删除它? 最佳答案 为此,PrimeFaces 在幕后使用 Apache Commons F
我尝试发送附件文件,但我得到 Cannot use object of type Illuminate\Http\UploadedFile as array 我使用 Laravel 5.4 有人知道我
有没有办法将字符串转换成ActionDispatch::Http::UploadedFile 我需要它能够在 API 请求中传递文件路径以处理将 excel 插入数据库,因为我想使用 rest API
我使用了与importing csv and excel Railscast类似的想法,但是由于该情节中的标准代码需要一些时间来处理(使用ActiveRecord为文件中的每一行创建新记录),我在He
我是一名优秀的程序员,十分优秀!