OphCoTherapyapplication_FileCollection::checkMimeType PHP Method

checkMimeType() public static method

function to check if a file is the right type to become part of a collection.
public static checkMimeType ( string $file ) : boolean
$file string
return boolean
    public static function checkMimeType($file)
    {
        $finfo = new finfo(FILEINFO_MIME_TYPE);
        return in_array($finfo->file($file), array('application/pdf'));
    }

Usage Example

示例#1
0
 /**
  * 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;
 }