createForWriting() public static method

create a new protected file object which has properties that can be used for writing an actual file to.
public static createForWriting ( string $name ) : ProtectedFile
$name string
return ProtectedFile
 public static function fromFhir($fhirObject)
 {
     $report = parent::fromFhir($fhirObject);
     $patient = \Patient::model()->find('id=?', array($report->patient_id));
     $report->patient_id = $patient->id;
     $eye = 'Right';
     if ($report->eye == 'L') {
         $eye = 'Left';
     } elseif ($report->eye == 'B') {
         $eye = 'Both';
     }
     $report->eye_id = \Eye::model()->find('name=:name', array(':name' => $eye))->id;
     if (isset($fhirObject->xml_file_data)) {
         $report->xml_file_data = base64_decode($fhirObject->xml_file_data);
     }
     $title = $report->file_reference;
     if (\ProtectedFile::model()->find('name = ?', array($title))) {
         throw new EverythingsFine("Duplicate filename: {$title} (patient ID {$report->patient_id})");
     }
     $protected_file = \ProtectedFile::createForWriting($title);
     $protected_file->name = $title;
     file_put_contents($protected_file->getPath(), base64_decode($report->image_scan_data));
     $protected_file->mimetype = 'image/gif';
     $protected_file->save();
     $cropped_file = \ProtectedFile::createForWriting($title);
     // all content is base64 encoded, so decode it:
     file_put_contents($cropped_file->getPath(), base64_decode($report->image_scan_crop_data));
     $cropped_file->mimetype = 'image/gif';
     $cropped_file->name = $title;
     $cropped_file->save();
     $report->scanned_field_id = $protected_file->id;
     $report->scanned_field_crop_id = $cropped_file->id;
     return $report;
 }
 /**
  * create the PDF file as a ProtectedFile for the given side.
  *
  * @param CController $controller
  * @param array       $template_data
  * @param string      $side
  *
  * @throws Exception
  *
  * @return ProtectedFile|null
  */
 protected function createAndSavePdfForSide(CController $controller, array $template_data, $side)
 {
     if ($html = $this->getPDFContentForSide($controller, $template_data, $side)) {
         $html = '<link rel="stylesheet" type="text/css" href="' . $controller->assetPath . '/css/print.css" />' . "\n" . $html;
         $this->event->lock();
         if (!$this->event->hasPDF('therapy_application')) {
             $wk = new WKHtmlToPDF();
             $wk->setDocuments(1);
             $wk->setDocRef($this->event->docref);
             $wk->setPatient($this->event->episode->patient);
             $wk->setBarcode($this->event->barcodeHTML);
             $wk->generatePDF($this->event->imageDirectory, 'event', 'therapy_application', $html, false, false);
         }
         $this->event->unlock();
         if (@$_GET['html']) {
             return Yii::app()->end();
         }
         $pfile = ProtectedFile::createForWriting('ECForm - ' . $side . ' - ' . $template_data['patient']->hos_num . '.pdf');
         if (!@copy($this->event->getPDF('therapy_application'), $pfile->getPath())) {
             throw new Exception('Unable to write to file: ' . $pfile->getPath());
         }
         if (!$pfile->save()) {
             throw new Exception('Unable to save file: ' . print_r($pfile->errors, true));
         }
         return $pfile;
     }
     return;
 }
 /**
  * get a compressed zip file containing all the files in the collection.
  *
  * @throws Exception
  *
  * @return ProtectedFile
  */
 public function getZipFile()
 {
     if (!$this->compressed_file) {
         // need to generate zip file, and store it with this collection
         $pfile = ProtectedFile::createForWriting($this->name . '.zip');
         $zip = new ZipArchive();
         if (!$zip->open($pfile->getPath(), ZIPARCHIVE::OVERWRITE)) {
             throw new Exception('cannot create zip file');
         }
         foreach ($this->files as $pf) {
             $zip->addFile($pf->getPath(), $pf->name);
         }
         $zip->close();
         $pfile->save();
         // set up relation
         $this->compressed_file = $pfile;
         $this->zipfile_id = $pfile->id;
         $this->save();
     }
     return $this->compressed_file;
 }