2012-09-14 36 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); 
    } 
}