2015-03-31 45 views
0

我讀了如何上傳圖片本文檔(http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html),但我只能一次創建它。第二次,create方法返回一個錯誤。我認爲這是我的實體的move方法返回錯誤。Symfony的2.6上傳文件無法創建...目錄

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上小牛和使用內部Apache服務器(不是甲基苯丙胺或WAMP或......)

我檢查了所有,似乎我有權寫入。我按照這個教程:http://paulmason.name/item/change-apache-user-group-in-lion-os-x

它給我寫如預期時,我做的回聲......

我感到奇怪的是,我已經有一個文件夾。爲什麼Symfony嘗試再次創建文件夾?

回答

0

也許您只上載文件夾改變chmods?請記住也要更改子文件夾的權限。在這種情況下,它是需要保存權限的媒體文件夾。要使chmod命令以遞歸方式工作,請添加-R參數。例如:

chmod -R +rw mydir 

更多信息:

man chmod 
+0

所有文件夾都是drwxrwxrwx。沒有什麼變化,但我問了一個問題。這是不是一個權限問題,因爲錯誤說我不能創建一個目錄,但它已經創建?每次上傳圖片時是否創建目錄?這對我沒有意義。謝謝@Ranker – 2015-04-01 06:32:15

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

__DIR__.'//../../../../../../web/'.$this->getUploadDir(); 
相關問題