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


Symfony 2.6 upload File unable to create the ... directory

我阅读了此文档(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 31 mar13: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再次尝试创建文件夹?

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

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

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

chmod -R +rw mydir

更多信息:

man chmod