2012-09-14 35 views
1

我一個使用圖像刪除此代碼時,我刪除的實體我怎樣才能取消鏈接Symfony2的文件中去除

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

但問題是,如果我沒有像有那麼拋出異常喜歡這

Warning: unlink(/home/site/../../../../uploads/50343885699c5.jpeg) [<a href='function.unlink'>function.unlink</a>]: No such file or directory i 

有沒有什麼辦法,如果文件不存在或目錄不存在,應該跳過這一步,還是刪除

回答

3

您可以使用file_exists,以確保日實體e文件實際上存在,並且is_writable以確保您有權刪除它。

if ($this->filenameForRemove) 
{ 
    if (file_exists($this->filenameForRemove) && 
     is_writable($this->filenameForRemove)) 
    { 
     unlink ($this->filenameForRemove); 
    } 
}