0

我阅读了有关如何上传图像的文档(http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html),但我只能创建一次。第二次,create 方法返回错误。我认为这move是我的实体的方法返回错误。

无法创建“/Users/......../Bundle/ArtworkBundle/Entity../../../../../../web/uploads/media”目录

当我查看带有错误的代码时,我认为这是权限问题。但我真的不知道如何改变这一点。

if (!is_dir($directory)) {
    if (false === @mkdir($directory, 0777, true)) {
        throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
    }
} elseif (!is_writable($directory)) {
    throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));

我正在尝试更改 chmod 权限,重新启动机器但没有任何变化:

drwxrwxrwx 4 blucas 员工 136 3 月 31 日 13:50 上传

我的实体图片的一部分

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

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

    /**
     * 
     * @return type
     */
    protected function getUploadRootDir()
    {
        // le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
        return __DIR__.'../../../../../../web/'.$this->getUploadDir();
    }

    /**
     * 
     * @return string
     */
    protected function getUploadDir()
    {
        // on se débarrasse de « __DIR__ » afin de ne pas avoir de problème lorsqu'on affiche
        // le document/image dans la vue.
        return 'uploads/media';
    }



    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->file) {
            // faites ce que vous voulez pour générer un nom unique
            $this->path = 'ctc-'.sha1(uniqid(mt_rand(), true)).'.'.$this->file->guessExtension();
        }
    }

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

        // s'il y a une erreur lors du déplacement du fichier, une exception
        // va automatiquement être lancée par la méthode move(). Cela va empêcher
        // proprement l'entité d'être persistée dans la base de données si
        // erreur il y a
        $this->file->move($this->getUploadRootDir(), $this->path);

        unset($this->file);
    }

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

我在 Mac Mavericks 上并使用内部 apache 服务器(不是 mamp 或 wamp 或......)

我检查了所有,似乎我有写的权限。我遵循本教程: http: //paulmason.name/item/change-apache-user-group-in-lion-os-x

当我做回声时,它给了我预期的写...

我觉得奇怪的是我已经有一个文件夹。为什么 Symfony 会再次尝试创建文件夹?

4

2 回答 2

1
__DIR__.'../../../../../../web/'.$this->getUploadDir();

__DIR__.'//../../../../../../web/'.$this->getUploadDir();
于 2015-04-20T14:51:52.090 回答
0

也许您只为上传文件夹更改了 chmods?记住也要更改子文件夹的权限。在这种情况下,需要保存权限的是媒体文件夹。要使 chmod 命令递归工作,请添加 -R 参数。例如:

chmod -R +rw mydir

更多信息:

man chmod
于 2015-03-31T13:04:28.847 回答