dongyun6229 2014-12-10 13:34
浏览 45

Symfony复制文件,如上传文件

I have a form that can upload an image to my website.

I have thousand images to upload in a folder (test images). I create a symfony Command to import and copy my images to the destination folder.

$url = '/tmp/testimg.jpg';
$photo = new Photo();
$photo->setName('test name');
$photo->setFile(new UploadedFile($url, basename($url)));
$photo->upload();

When i execute the commande i have:

[Symfony\Component\HttpFoundation\File\Exception\FileException]
The file "test name" was not uploaded due to an unknown error.

Function Photo::upload:

public function upload()
{
  if (null === $this->file) {
    return;
  }
  $this->file->move(
      "/home/julien/work/mysite/src/MyProject/PhotoBundle/Entity/../../../../web/uploads/photos",
  $this->name
);
  $this->file = null;
} 
  • 写回答

1条回答

  • dphs48626 2014-12-10 15:02
    关注

    This looks like the move method crashes. Using absolute paths might be a problem here. You could try this as well:

    __DIR__ . '/../../../../web/uploads/photos'
    

    Also make sure that you have proper access to that folder.

    I'd also attach a debugger to this just to be sure. You can easily set up a debug session using XDebug and step through your lines. It's always good to step into each function of your code and see where it breaks.

    If your code still fails I'd suggest you implement this example and see if it works:

    http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算