如果你想删除图像试试这个
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());
}
}