我的应用有一个“Photo
”字段来存储网址。它使用sfWidgetFormInputFileEditable
作为小部件架构。要在上传新图像时删除旧图像,我在设置过度设置器中的值之前使用取消链接,它可以正常工作!!!
if (file_exists($this->_get('photo')))
unlink($this->_get('photo'));
Photo
”时,仅保存文件名xxx-yyy.zzz
(而不是完整路径)。但是,我想知道symfony / php如何知道要删除的文件的完整路径?第2部分:
我正在使用sfThumbnailPlugin
生成缩略图。所以实际代码如下所示:
public function setPhoto($value)
{
if(!empty($value))
{
Contact::generateThumbnail($value); // delete current Photo & create thumbnail
$this->_set('photo',$value); // setting new value after deleting old one
}
}
public function generateThumbnail($value)
{
$uploadDir = sfConfig::get('app_photo_upload'); // path to upload folder
if (file_exists($this->_get('photo')))
{
unlink($this->_get('photo')); // delete full-size image
// path to thumbnail
$thumbpath = $uploadDir.'/thumbnails/'.$this->get('photo');
// read a blog, tried setting dir manually, doesn't work :(
//chdir('/thumbnails/');
// tried closing the file too, doesn't work! :(
//fclose($thumbpath) or die("can't close file");
//unlink($this->_get('photo')); // doesn't work; no error :(
unlink($thumbpath); // doesn't work, no error :(
}
$thumbnail = new sfThumbnail(150, 150);
$thumbnail->loadFile($uploadDir.'/'.$value);
$thumbnail->save($uploadDir.'/thumbnails/'.$value, 'image/png');
}
sfWidgetFormInputFileEditable
窗口小部件中吗?我花了好几个小时试图解决这个问题,但无法确定真正的原因。 提前谢谢。
答案 0 :(得分:2)
$ path =“uploads / photos”; $ image =“图像名称”;
unlink($path.$image);
答案 1 :(得分:1)
解决&惊讶
好的,这是添加了一些回声后的代码......
public function generateThumbnail($value)
{
$uploadDir = sfConfig::get('app_photo_upload'); // path to upload folder
// path to thumbnail
$thumbpath = $uploadDir.'/thumbnails/'.$this->get('photo');
if (file_exists($uploadDir.$this->_get('photo')))
{
>> unlink($uploadDir.$this->_get('photo')); // delete full-size image
>> unlink($thumbpath); // delete thumn
}
//thumbnail generation code
}
据说,unlink($this->_get('photo'))
从未奏效。事实上,从未输入if(fileExists)块,但文件已被删除。
我认为sfWidgetFormInputFileEditable
在上传新图片时会自动删除完整图片。
感谢Martin&卡纳克
答案 2 :(得分:0)
你确定这条线吗?
$thumbpath = $uploadDir.'/thumbnails/'.$this->get('photo');
我认为您应该使用$this->_get('photo');
代替$this->get('photo');
也许尝试转储$thumbpath
变量。
var_dump($thumbpath); exit;