2013-07-09 42 views
0

如DOC這裏所說的正常工作是我的removeUpload()方法:文件取消鏈接不會被給定的方向

public function removeUpload() 
{ 
    if ($file = $this->getAbsolutePath()) { 
     unlink($file); 
    } 
} 

這裏就是我將其刪除:

$image = $this->getDoctrine()->getRepository('GreenMonkeyDevGlassShopBundle:ProductImages')->find($product_image_id); 
    $image->removeUpload(); 

這是我的錯誤:

Warning: unlink(/var/www/gmd-milkywayglass/src/GreenMonkeyDev/GlassShopBundle/Entity/../../../../web/images/product_images/images/product_images/PAid.png): No such file or directory in /var/www/gmd-milkywayglass/src/GreenMonkeyDev/GlassShopBundle/Entity/ProductImages.php line 168`

這對我來說很有意義:即它看起來在DIR該取消鏈接的方法是我斷開鏈接但是,爲什麼以及如何改變這種情況?

試圖unlink(realpath($file));得到錯誤:

Warning: unlink(): No such file or directory in /var/www/gmd- 
Milkywayglass/src/GreenMonkeyDev/GlassShopBundle/Entity/ 
ProductImages.php line 168 

而且它確實有正確的權限設置。所以我很茫然。

回答

1

如果你想刪除的圖像試試這個

public function deleteimgAction(image $image) 
    { 


     $em = $this->getDoctrine()->getEntityManager(); 

     $em->remove($image); 
     $em->flush(); 

     return $this->redirect($this->generateUrl('rederect..')); 
    } 

,並在圖像實體您添加有無cyclecallback添加一條自動觸發器,刪除圖像當你刪除數據庫中的記錄時

/** 
* Image 
* @ORM\HasLifecycleCallbacks 
* @ORM\Table(name="image") 
* @ORM\Entity(repositoryClass="Elite\AdminBundle\Entity\ImageRepository") 
*/ 
class Image 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * @Assert\File(maxSize="6000000") 
    * @ORM\Column(name="image", type="string", length=255) 
    */ 
    private $image; 




    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set image 
    * 
    * @param string $image 
    * @return Image 
    */ 
    public function setImage($image) 
    { 
     $this->image = $image; 

     return $this; 
    } 



    /** 
    * Get image 
    * 
    * @return string 
    */ 
    public function getImage() 
    { 
     return $this->image; 
    } 


    public function getFullImagePath() { 
     return null === $this->image ? null : $this->getUploadRootDir(). $this->image; 
    } 

    protected function getUploadRootDir() { 
     // the absolute directory path where uploaded documents should be saved 
     return $this->getTmpUploadRootDir(). $this->getAlbum() ."/"; 
    } 

    protected function getTmpUploadRootDir() { 
     // the absolute directory path where uploaded documents should be saved 
     return __DIR__ . '/../../../../web/upload/albums/'; 
    } 







    /** 
    * @ORM\PrePersist() 
    * @ORM\PreUpdate() 
    */ 
    public function uploadImage() { 

     // the file property can be empty if the field is not required 
     if (null === $this->image) { 
      return; 
     } 
     $name=preg_replace('/[^\w\._]+/', '_',$this->image->getClientOriginalName()); 
     if(!$this->id){ 
      $this->image->move($this->getTmpUploadRootDir(), $name); 
     }else{ 
      $this->image->move($this->getUploadRootDir(), $name); 
     } 

     $this->setImage($name); 

    } 

    /** 
    * @ORM\PostPersist() 
    */ 
    public function moveImage() 
    { 
     if (null === $this->image) { 
      return; 
     } 
     if(!is_dir($this->getUploadRootDir())){ 
      mkdir($this->getUploadRootDir()); 
     } 
     copy($this->getTmpUploadRootDir().$this->image, $this->getFullImagePath()); 
     $fullimage=$this->getFullImagePath(); 
     $filename="$fullimage"; 
     $parts=pathinfo($filename); 
     $ext=$parts['extension']; 
     if($ext == 'jpg' or $ext == 'jpeg'){ 

      $img = imagecreatefromjpeg($filename); 
      header("Content-Type: image/jpeg"); 
      imagejpeg($img, $filename, 70); 
     } 
     if($ext == 'png'){ 

      $img = imagecreatefrompng($filename); 
      imagealphablending($img, true); 
      imagesavealpha($img, true); 
      header("Content-Type: image/png"); 
      imagepng($img, $filename, 8); 
     } 
     unlink($this->getTmpUploadRootDir().$this->image); 
    } 

    /** 
    * @ORM\PreRemove() 
    */ 
    public function removeImage() 
    { 
     unlink($this->getFullImagePath()); 

    } 



} 
1

使用PHP的功能realpath

public function removeUpload() 
{ 
    if ($file = $this->getAbsolutePath()) { 
     unlink(realpath($file)); 
    } 
} 
+0

試過了,'realpa它認爲是在實體目錄...?但接近! – TooTiredToDrink

相關問題