Symfony 2.6上传文件无法创建...目录

时间:2015-03-31 12:18:42

标签: php file-upload symfony-2.6

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

Unable to create the "/Users/......./Bundle/ArtworkBundle/Entity../../../../../../web/uploads/media" directory

我认为当我查看错误给出的代码时,这是一个许可问题。但我真的不知道如何改变这一点。

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  staff    136 31 mar 13:50 uploads

我的实体部分图片

/**
     * 
     * @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会再次尝试创建一个文件夹?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

chmod -R +rw mydir

更多信息:

man chmod
相关问题