views:

299

answers:

3

My app has a 'Photo' field to store URL. It uses sfWidgetFormInputFileEditable for the widget schema. To delete the old image when a new image is uploaded, I use unlink before setting the value in the over-ridden setter and it works!!!

if (file_exists($this->_get('photo')))
    unlink($this->_get('photo'));
  1. Photos are stored in uploads/photos and when saving 'Photo' only the file name xxx-yyy.zzz is saved (and not the full path). However, I wish to know how symfony/php knows the full path of the file to be deleted?

Part 2: I am using sfThumbnailPlugin to generate thumbnails. So the actual code looks like this:

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');
    }
  1. Why can't the thumbnail be deleted using unlink()? is the sequence of ops incorrect? Is it because the old thumbnail is displayed in the sfWidgetFormInputFileEditable widget?

I've spent hours trying to figure this out, but unable to nail down the real cause. Thanks in advance.

A: 

Are you sure about this line?

$thumbpath = $uploadDir.'/thumbnails/'.$this->get('photo');

I think you should use $this->_get('photo'); instead of $this->get('photo');

Maybe try to dump $thumbpath variable.

var_dump($thumbpath); exit;
Martin Sikora
+1  A: 

Hello,

$path = "uploads/photos"; $image = "Name of image";

unlink($path.$image);

Kanak Vaghela
+1  A: 

Solved & Surprised

OK, here is the code that worked after adding some echoes...

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
    }

Supposedly, the unlink($this->_get('photo')) never worked. Infact, the if(fileExists) block was never entered and yet the file was deleted. I think sfWidgetFormInputFileEditable was deleting the full-image automatically when a new one was being uploaded.

Thanks Martin & Kanak

Prasad