Example #1
0
 /**
  * The function attaches file to current Object by index.
  * 
  * @static
  * @access public
  * @param object $Object The object.
  * @param mixed $file The array of File data or path to file on server.
  * @return bool TRUE on success, FALSE on failure.
  */
 public static function attach(Object $Object, $file, $index = null)
 {
     $info = $Object->getUploadFileInfo();
     if (!is_array($info) || !$Object->Id) {
         self::$uploadError = $Object->Id ? 'Upload file info is not defined in model' : 'Object Id is not defined';
         return false;
     }
     self::restore(self::path($Object, $index));
     if ($index === null) {
         $index = 'orig';
     }
     $name = is_array($file) ? $file['name'] : basename($file);
     $source = is_array($file) ? $file['tmp_name'] : $file;
     $ext = self::extension($name, true);
     if (!self::checkExtension($info, $ext)) {
         return false;
     }
     if ($index === 'orig') {
         if (property_exists($Object, 'Filename')) {
             $Object->Filename = $name;
         }
         if (property_exists($Object, 'Extension')) {
             $Object->Extension = $ext;
         }
         if (property_exists($Object, 'Filesize')) {
             $Object->Filesize = filesize($source);
         }
     }
     if (is_array($file)) {
         $ok = move_uploaded_file($file['tmp_name'], self::path($Object, $index));
     } else {
         $ok = rename($file, self::path($Object, $index));
     }
     if (!$ok) {
         self::$uploadError = 'Could not move/copy uploaded file';
         return false;
     }
     if ($index === 'orig') {
         if (property_exists($Object, 'IsFile')) {
             $Object->IsFile = 1;
         }
     }
     return true;
 }