createFromFile() public static méthode

Create a new protected file from an existing file.
public static createFromFile ( string $path ) : ProtectedFile
$path string Path to file
Résultat ProtectedFile
 public function actionImport()
 {
     echo "<h1>Importing files:</h1>";
     foreach (glob(Yii::app()->basePath . '/data/test/*') as $src_file) {
         $file = ProtectedFile::createFromFile($src_file);
         if (!$file->save()) {
             throw new CException('Cannot save file' . print_r($file->getErrors(), true));
         }
         unlink($src_file);
         echo "<p>Imported " . $file->uid . ' - ' . $file->name . "</p>";
     }
     echo "<p>Done!</p>";
 }
 /**
  * processes uploaded files for a collection - will add errors to the collection if there are problems
  * with any of the files. Otherwise returns array of protected file ids that have been created.
  *
  * @param $collection
  * @param $uploaded_files
  *
  * @throws Exception
  *
  * @return array $protectedfile_ids
  */
 protected function processFileCollectionFileUpload($collection, $uploaded_files)
 {
     $files = array();
     foreach ($uploaded_files['tmp_name'] as $i => $f) {
         if (!empty($uploaded_files['error'][$i])) {
             $collection->addError('files', "file {$i} had an error");
         } elseif (!empty($f) && is_uploaded_file($f)) {
             $name = $uploaded_files['name'][$i];
             // check the file mimetype
             if (OphCoTherapyapplication_FileCollection::checkMimeType($f)) {
                 $files[] = array('tmpfile' => $f, 'name' => $name);
             } else {
                 $collection->addError('files', "File {$name} is not a valid filetype");
             }
         }
     }
     $pfs = array();
     $pf_ids = array();
     if (!count($collection->getErrors())) {
         foreach ($files as $fdet) {
             $pf = ProtectedFile::createFromFile($fdet['tmpfile']);
             $pf->name = $fdet['name'];
             if ($pf->save()) {
                 $pfs[] = $pf;
                 $pf_ids[] = $pf->id;
             } else {
                 $collection->addError('files', 'There was a problem storing file ' . $pf->name);
                 Yii::log("couldn't save file object" . print_r($pf->getErrors(), true), 'error');
                 // need to remove any protected files that have been created so far (note that because
                 // ProtectedFile affects the filesystem, we are relying on the delete clean up method)
                 foreach ($pfs as $pf) {
                     $pf->delete();
                 }
                 // return an empty array - no protected files successfully created.
                 return array();
             }
         }
     }
     return $pf_ids;
 }
 /**
  * main method to run the command for file collection creation.
  *
  * @TODO: look for a summary text file to include.
  * @TODO: search for existing file collections and update instead of adding.
  *
  * @param array $args
  *
  * @return int|void
  */
 public function run($args)
 {
     if (!count($args) == 1) {
         $this->usageError('missing source path argument');
     }
     if (!is_readable($args[0])) {
         $this->usageError('cannot read specified source path ' . $args[0]);
     }
     $base_path = $args[0];
     // read directory structure into data
     $file_list = $this->buildFileList($base_path, './');
     $file_ext_regexp = implode('|', $this->file_extensions);
     $sets = array();
     // determine the file collections to be created
     foreach ($file_list as $fname => $details) {
         if (preg_match('/' . $file_ext_regexp . '$/', $fname)) {
             $path = str_replace(DIRECTORY_SEPARATOR, ' - ', dirname($fname));
             if (!@$sets[$path]) {
                 $summary_text = $this->summary_text_default;
                 $summary_filepath = $base_path . dirname($fname) . DIRECTORY_SEPARATOR . $this->summary_filename;
                 if ($this->summary_filename && file_exists($summary_filepath)) {
                     // read the summary text in from the file
                     $summary_text = file_get_contents($summary_filepath);
                 }
                 $sets[$path] = array('summary' => $summary_text, 'files' => array($details));
             } else {
                 $sets[$path]['files'][] = $details;
             }
         }
     }
     $created = 0;
     $modified = 0;
     // iterate through and create the file collections.
     foreach ($sets as $set_name => $set_details) {
         $created_flag = false;
         $transaction = Yii::app()->getDb()->beginTransaction();
         $pf_list = array();
         $pf_ids = array();
         try {
             foreach ($set_details['files'] as $details) {
                 $pf = ProtectedFile::createFromFile($details['source']);
                 if ($pf->save()) {
                     $pf_ids[] = $pf->id;
                     $pf_list[] = $pf;
                 } else {
                     foreach ($pf_list as $pf) {
                         $pf->delete();
                     }
                     break;
                 }
             }
             // update the existing file collection if there is one
             $criteria = new CDbCriteria();
             $criteria->addCondition('name = :nm');
             $criteria->params = array(':nm' => $set_name);
             if (!($fc = OphCoTherapyapplication_FileCollection::model()->find($criteria))) {
                 $fc = new OphCoTherapyapplication_FileCollection();
                 $fc->name = $set_name;
                 $created_flag = true;
             }
             $fc->summary = $set_details['summary'];
             if (!$fc->validate()) {
                 echo "unexpected validation error with file collection\n";
                 var_dump($fc->getErrors());
                 $transaction->rollback();
             } else {
                 if ($fc->save()) {
                     $fc->updateFiles($pf_ids);
                     Audit::add('admin', 'create', $fc->id, null, array('module' => 'OphCoTherapyapplication', 'model' => 'OphCoTherapyapplication_FileCollection'));
                     $transaction->commit();
                     $created_flag ? $created++ : $modified++;
                 } else {
                     foreach ($pf_list as $pf) {
                         $pf->delete();
                     }
                     $transaction->rollback();
                 }
             }
         } catch (Exception $e) {
             echo $e->getMessage();
             foreach ($pf_list as $pf) {
                 $pf->delete();
             }
             $transaction->rollback();
         }
     }
     echo 'Processing complete, ' . $created . ' collections created, ' . $modified . " collections updated\n";
 }
 /**
  * Store the PDF as a ProtectedFile in the system
  *
  * @return ProtectedFile
  * @throws Exception
  */
 public function storePDF()
 {
     $pf = ProtectedFile::createFromFile($this->outDir . '/' . $this->outFile);
     $pf->save();
     $this->cleanUp();
     return $pf;
 }