Search This Blog

Thursday, December 16, 2010

How to download files in Symfony

Its very easy to download files in symfony. Create a proper action and do something like this , i have assumes that files are stored in the database and its a pdf file. But you can assign any file by storing the file type in the database.



public function  executeDownloadFile(sfWebRequest $request) {
        $yourfileData = '' // get your file data from the database
        $this->getResponse()->clearHttpHeaders();
        $this->getResponse()->setHttpHeader('Content-Disposition',
        'attachment; filename='. 'myfile.pdf');
        $this->getResponse()->setContentType('application/pdf');
        $this->getResponse()->sendHttpHeaders();       
        $this->getResponse()->setContent($yourfileData);

        return sfView::NONE;

}

If you need to set http headers 

private function setHttpHeaders($size, $fileName, $contentType='application/csv') {
        $this->getResponse()->clearHttpHeaders();
        $this->getResponse()->addCacheControlHttpHeader('Cache-control', 'private');
        $this->getResponse()->setHttpHeader('Content-Description', 'File Transfer');
        $this->getResponse()->setContentType($contentType, TRUE);
        $this->getResponse()->setHttpHeader('Content-Length', (string) $size, TRUE);
        $this->getResponse()->setHttpHeader('content-transfer-encoding', 'binary', TRUE);
        $this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename=' . $fileName, TRUE);
        $this->getResponse()->sendHttpHeaders();
    }

Tuesday, December 14, 2010

Recursively deletes subversion .svn folders in ubuntu

First we find the .svn folders, then we use rm to remove those folders . There are 2 methods to do this.Type this in shell


Method -1

$ find . -name ".svn" -exec rm -rf {} \;
Method -2

$ rm -rf `find . -type d -name .svn`

Sunday, December 5, 2010

Save new record or Update a record if it exist using Doctrine object

Are getting error when you save a doctrine object saying primary key violation?
Before you save the object you must search if its already there. When you use find it uses the primary key.

public function saveFamilyDetails($form) {

$employeeFamilyDetails = Doctrine::getTable('EmployeeFamilyDetails')->find($form['txtFamilyID']);

        if(!($employeeFamilyDetails instanceof EmployeeFamilyDetails) ) {
            $employeeFamilyDetails = new EmployeeFamilyDetails();
        }

        $employeeFamilyDetails->employee_id       = $form['txtEmpID'];
        $employeeFamilyDetails->fm_name       = $form['txtFamName'];
        $employeeFamilyDetails->relationship       = $form['txtFamRelationship'];
        $employeeFamilyDetails->date_of_birth        = $form['txtFamDob'];
        $employeeFamilyDetails->address        = $form['txtFamAddress'];
        $employeeFamilyDetails->mobile_no      = $form['txtFamMobileNo'];
        $employeeFamilyDetails->residence_no    = $form['txtFamRecidenNo'];
        $employeeFamilyDetails->workcon_no   = $form['txtFamWorkConNo'] ;
        $employeeFamilyDetails->dependent      = $form['txtFamDependant'];
        $employeeFamilyDetails->nric_fin    = $form['txtFamNricFin'] ;
        $employeeFamilyDetails->passpot_no   = $form['txtFamPassport'] ;

        return $employeeFamilyDetails->save();



    }