【问题标题】:Symfony2 - 2.6 file uploadsSymfony2 - 2.6 文件上传
【发布时间】:2015-02-27 17:36:37
【问题描述】:

我在 2.6 上遇到文件上传问题,在以前的版本上运行良好。我不知道问题是什么,没有错误,当我转储帖子对象时,我在那里看到一个文件名,但图像文件夹中没有文件。

我的图片文件夹在 myproject/web/images 中。

使用 SonataAdmin 上传文件,同样没有错误。

谁能指出我做错了什么或发生了什么变化?

编辑:仔细阅读文件夹后,我注意到它在 /var/www/html 中创建了另一个 web/images 文件夹。

知道如何正确吗?我的项目文件夹在 /var/www/html/myproject 中。

尝试再添加 2 个 /../../ 但出现错误:Unable to create the "/var/www/html/myproject/src/AppBundle/Entity/../../../../../../web/images/" directory。并删除了 2 /../../ 仍然没有。

使用以下命令在 twig 中调用文件:

{% for photo in photos %}
    <a href="{{ asset(['images/', photo.image]|join) }}">
        <img src="{{ asset(['images/', photo.image]|join) }}" width="400" height="600" alt=""/>
    </a>
{% endfor %}

发布实体

/**
 * @var string
 *
 * @ORM\Column(name="file", type="string", length=255)
 */
private $image;

public $file;


/**
 * Set image
 *
 * @param string $image
 * @return Post
 */
public function setImage($image)
{
    $this->image = $image;

    return $this;
}

/**
 * Get image
 *
 * @return string
 */
public function getImage()
{
    return $this->image;
}

public function getUploadDir()
{
    return 'images/';
}

public function getUploadRootDir()
{
    return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}

public function getWebPath()
{
    return null === $this->image ? null : $this->getUploadDir() . '/' . $this->image;
}

public function getAbsolutePath()
{
    return null === $this->image ? null : $this->getUploadRootDir() . '/' . $this->image;
}

/**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
public function preUpload()
{
    if (null !== $this->file) {
        $this->image = uniqid() . '.' . $this->file->guessExtension();
    }
}

/**
 * @ORM\PostPersist()
 * @ORM\PostUpdate()
 */
public function upload()
{
    if (null === $this->file) {
        return;
    }

    // If there is an error when moving the file, an exception will
    // be automatically thrown by move(). This will properly prevent
    // the entity from being persisted to the database on error
    $this->file->move($this->getUploadRootDir(), $this->image);

    $this->file = null;
}

/**
 * @ORM\PostRemove()
 */
public function removeUpload()
{
    if ($file = $this->getAbsolutePath()) {
        unlink($file);
    }
}

【问题讨论】:

  • myproject/web/images 是可写的吗?你给了它什么权限?
  • 是的。将所有者更改为 www-data:www-data,权限为 775。
  • 你能试试这个,让我们知道它是否工作 public function getUploadDir() { return 'images'; //删除尾部斜杠(/) }
  • 是的,先生,我也试过了,还是不行。
  • 还是把文件上传到了错误的目录。 /var/www/html/web/images 而不是 /var/www/html/myproject/web/images

标签: symfony file-upload


【解决方案1】:

您的实体路径是:

var/www/html/myproject/src/AppBundle/Entity/Post.php

还有你的getUploadRootDir 方法:

public function getUploadRootDir()
{
    return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}

这将返回四次,因为有 4(../) 将您带出源目录。

用这个来解决这个错误:

public function getUploadRootDir()
{
    return __DIR__ . '/../../../web/' . $this->getUploadDir(); 
}

【讨论】:

  • 感谢您的耐心和帮助。非常感谢。
【解决方案2】:

用这个改变你的实体。

代替

public function getUploadDir()
{
    return 'images/';
}

替换这个

public function getUploadDir()
{
    return '/images';
}

【讨论】:

  • 试过了,不行。甚至尝试在网络'/../../../../web' 上保留/删除尾部斜杠,但仍然没有。它仍然上传到var/www/html/web/images
  • 你想在哪个目录上传你的图片文件?对吗?
  • 如果是,那么只需在网络上再创建一个目录上传并替换.... public function getUploadDir() { return 'Upload/images'; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-18
  • 2012-04-15
  • 2015-10-16
  • 1970-01-01
  • 1970-01-01
  • 2010-11-05
相关资源
最近更新 更多