0

如文档中所述,这是我的removeUpload()方法:

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

这是我删除它的地方:

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

这是我的错误:

警告:取消链接(/var/www/gmd-milkywayglass/src/GreenMonkeyDev/GlassShopBundle/Entity/../../../../web/images/product_images/images/product_images/PAid.png):没有/var/www/gmd-milkywayglass/src/GreenMonkeyDev/GlassShopBundle/Entity/ProductImages.php 第 168 行中的文件或目录`

这对我来说很有意义:它希望在 unlink 方法所在的目录中取消链接。但是为什么以及如何更改它?

尝试unlink(realpath($file));得到错误:

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

它也确实设置了适当的权限。所以我很茫然。

4

2 回答 2

1

如果你想删除图像试试这个

public function deleteimgAction(image $image)
    {


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

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

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

并在图像实体中添加有循环回调以添加自动触发器,当您删除数据库中的记录时删除图像

/**
 * 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());

    }



}
于 2013-07-10T01:30:47.837 回答
1

使用PHP的函数realpath

public function removeUpload()
{
    if ($file = $this->getAbsolutePath()) {
        unlink(realpath($file));
    }
}
于 2013-07-09T20:58:23.700 回答