如文档中所述,这里是我的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-milkywaylass/src/GreenMonkeyDev/GlassShopBundle/Entity/../../web.images/product_images/PAid.png
这对我来说是有道理的:它希望在unlink方法所在的目录中取消链接。但我为什么以及如何更改它?
尝试unlink(realpath($file));
获取错误:
Warning: unlink(): No such file or directory in /var/www/gmd-
Milkywayglass/src/GreenMonkeyDev/GlassShopBundle/Entity/
ProductImages.php line 168
此外,它确实设置了适当的权限。所以我不知所措。
如果您想删除图像,请尝试此
public function deleteimgAction(image $image)
{
$em = $this->getDoctrine()->getEntityManager();
$em->remove($image);
$em->flush();
return $this->redirect($this->generateUrl('rederect..'));
}
在图像实体中,您添加have 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());
}
}
使用PHP的函数realpath
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink(realpath($file));
}
}