Ejemplo n.º 1
0
 public static function getUrlFile($url, $bType)
 {
     if ($content = file_get_contents($url)) {
         $fileNode = explode('.', $url);
         $extension = $fileNode[count($fileNode) - 1];
         if (!in_array($extension, self::$fileExtension)) {
             $extension = 'jpg';
         }
         $fileNode = explode('/', $url);
         $fileName = $fileNode[count($fileNode) - 1];
         $fileMd5 = md5($content);
         $c = new EMongoCriteria();
         $c->hash = $fileMd5;
         $isExists = Attach::model()->find($c);
         if ($isExists) {
             return $isExists->_id;
         }
         $fileNameMd5 = md5($fileName . time());
         $fileRePath = substr($fileNameMd5, 0, 1) . '/' . substr($fileNameMd5, 1, 3) . '/' . substr($fileNameMd5, 4, 5);
         $fileNameMd5 = substr($fileNameMd5, 9, strlen($fileNameMd5));
         $static = Yii::app()->params['static'];
         $uploadPath = $static['uploadPath'];
         $filePath = $uploadPath . '/' . $fileRePath;
         $newFile = $filePath . '/' . $fileNameMd5 . '.' . $extension;
         self::createDir($filePath);
         if ($size = file_put_contents($newFile, $content)) {
             $newAttach = new Attach();
             $newAttach->uploadType = $bType;
             $newAttach->type = mime_content_type($newFile);
             $newAttach->creator = Yii::app()->user->getId();
             $newAttach->name = $fileName;
             $newAttach->size = $size;
             $newAttach->extension = $extension;
             $newAttach->hash = $fileMd5;
             $newAttach->isdel = 0;
             $newAttach->savepath = $fileRePath;
             $newAttach->savename = $fileNameMd5 . '.' . $extension;
             $newAttach->action = 0;
             $newAttach->save();
             return $newAttach->_id;
         }
     }
     return 0;
 }