コード例 #1
0
 public static function saveUploadedFile($fileupload, $dir, $old_file = '')
 {
     //print_r($fileupload);
     //Yii::app()->end();
     if ($fileupload instanceof CUploadedFile) {
         $newName = self::freeFileName(STranslate::transliter($fileupload->getName()), $dir);
         if (!file_exists($dir)) {
             mkdir($dir, 0775, true);
         }
         $fileupload->saveAs($dir . '/' . $newName);
         //$this->file = $newName;
         if (!empty($old_file)) {
             $delete = $dir . '/' . $old_file;
             if (file_exists($delete)) {
                 unlink($delete);
             }
         }
     }
     if (empty($newName) && !empty($old_file)) {
         $newName = $old_file;
     }
     return $newName;
 }
コード例 #2
0
 public function beforeSave()
 {
     if ($this->fileupload instanceof CUploadedFile) {
         $mainName = $newName = STranslate::transliter($this->fileupload->getName());
         $i = 1;
         while (file_exists($this->getPathDirStoredFile() . '/' . $newName)) {
             $parts = explode('.', $mainName);
             $countParts = count($parts);
             if ($countParts > 1) {
                 unset($parts[$countParts - 1]);
                 $newName = implode($parts) . '(' . $i . ').' . $this->fileupload->extensionName;
             } else {
                 $newName = '(' . $i . ')' . $mainName;
             }
             $i++;
         }
         $this->fileupload->saveAs($this->getPathDirStoredFile() . '/' . $newName);
         $this->file = $newName;
         if (!empty($this->old_file)) {
             $delete = $this->getPathDirStoredFile() . '/' . $this->old_file;
             if (file_exists($delete)) {
                 unlink($delete);
             }
         }
     }
     if (empty($this->file) && !empty($this->old_file)) {
         $this->file = $this->old_file;
     }
     return parent::beforeSave();
 }