ProtectedFile::createForWriting PHP Method

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 createForWriting($name)
    {
        $file = new self();
        $file->name = $name;
        $file->generateUID();
        $path = $file->getFilePath();
        if (!file_exists($path)) {
            if (!@mkdir($path, 0755, true)) {
                throw new Exception("{$path} could not be created: permission denied");
            }
        }
        return $file;
    }

Usage Example

 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;
 }
All Usage Examples Of ProtectedFile::createForWriting