Exemple #1
0
 /**
  * 检查文件是否真的是图片文件
  *
  * @param
  *            $filename
  * @param null $file_mime_type            
  * @param null $mime_type_list            
  *
  * @return bool
  */
 public static function isRealImage($filename, $file_mime_type = null, $mime_type_list = null)
 {
     // Detect mime content type
     $mime_type = false;
     if (!$mime_type_list) {
         $mime_type_list = array('image/gif', 'image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png');
     }
     $mime_type = Util_File::getMimeType($filename);
     if ($file_mime_type && (empty($mime_type) || $mime_type == 'regular file' || $mime_type == 'text/plain')) {
         $mime_type = $file_mime_type;
     }
     // For each allowed MIME type, we are looking for it inside the current MIME type
     foreach ($mime_type_list as $type) {
         if (strstr($mime_type, $type)) {
             return true;
         }
     }
     return false;
 }
Exemple #2
0
 /**
  * 保存上传的文件
  *
  * @param string $p_sName            
  * @param string $p_sTmpName            
  * @param int $p_iError            
  * @param int $p_iSize            
  * @param string $p_sIP            
  * @param int $p_iTime            
  * @param array $o_aFileInfo            
  * @return array
  */
 public function saveFile($p_sName, $p_sTmpName, $p_iError, $p_iSize, $p_sIP, $p_sDomain, $i_bID = Model_FileMeta::BID_DEFAULT)
 {
     $iError = self::FILE_SAVE_SUCCESS;
     do {
         if ($p_iError > 0) {
             // 文件上传出错
             $iError = self::FILE_UPLOAD_ERROR;
             $mReturn = $this->getUploadErrMsg($p_iError);
             break;
         }
         if (!is_uploaded_file($p_sTmpName)) {
             $iError = self::FILE_NOT_POST_UPLOAD_FILE;
             $mReturn = 'upload error, use http post to upload';
             break;
         }
         // 上传域名是否允许上传
         list($iError, $mReturn) = $this->checkAllowedSize($p_iSize, $p_sDomain, $iError);
         if (!empty($iError)) {
             break;
         }
         $sMimeType = Util_File::getMimeType($p_sTmpName);
         if (empty($sMimeType) || $sMimeType == 'application/zip') {
             $sMimeType = Util_File::getMimeTypeByExtension($p_sName);
         }
         if (empty($sMimeType)) {
             $sMimeType = 'image/jpeg';
         }
         $sExtension = Util_File::getExtension($sMimeType);
         // 上传文件类型
         list($iError, $mReturn) = $this->checkAllowedType($sExtension, $p_sDomain, $iError);
         if (!empty($iError)) {
             break;
         }
         $sFileKey = sha1_file($p_sTmpName);
         $aFileInfo = Model_File::getFileByKey($sFileKey);
         if (!empty($aFileInfo)) {
             $iError = self::FILE_SAVE_SUCCESS;
             $aFileMeta = Model_FileMeta::getFileMetaByKey($sFileKey);
             $aFileMet['iBID'] = $aFileMeta['iBID'] | $i_bID;
             Model_FileMeta::updData(array('iBID' => $aFileMet['iBID'], 'iAutoID' => $aFileMeta['iAutoID']));
             $mReturn = $this->normalizeFileInfo($aFileInfo, $aFileMeta);
             break;
         }
         $sDestFile = $this->getDestFile($sFileKey);
         $bImage = $this->isImage($sExtension);
         if ($bImage) {
             $aImageInfo = $this->getImageInfo($p_sTmpName);
             $o_aFileInfo = array('sKey' => $sFileKey, 'sExt' => $sExtension, 'sFile' => $sFileKey . '.' . $sExtension, 'iSize' => $p_iSize, 'iWidth' => $aImageInfo['iWidth'], 'iHeight' => $aImageInfo['iHeight']);
             $aFileMeta = array('sKey' => $sFileKey, 'sName' => $p_sName, 'iSize' => $p_iSize, 'sMimeType' => $sMimeType, 'iBID' => $i_bID, 'iIP' => $this->ipToLong($p_sIP), 'iWidth' => $aImageInfo['iWidth'], 'iHeight' => $aImageInfo['iHeight']);
         } else {
             $o_aFileInfo = array('sKey' => $sFileKey, 'sExt' => $sExtension, 'sFile' => $sFileKey . '.' . $sExtension, 'iSize' => $p_iSize);
             $aFileMeta = array('sKey' => $sFileKey, 'sName' => $p_sName, 'iSize' => $p_iSize, 'sMimeType' => $sMimeType, 'iBID' => $i_bID, 'iIP' => $this->ipToLong($p_sIP));
         }
         // 移动文件
         if (false === move_uploaded_file($p_sTmpName, $sDestFile)) {
             $iError = self::FILE_MOVE_TMP_FILE_ERROR;
             $mReturn = "Can not move upload file";
             break;
         }
         // 存储索引
         $iHostID = $this->getDispatchHostID($sFileKey);
         $aFile = array('sKey' => $sFileKey, 'sExt' => $sExtension, 'iHostID' => $iHostID);
         $iFileID = Model_File::addData($aFile);
         $iFileMetaID = Model_FileMeta::addData($aFileMeta);
         if (empty($iFileID) || empty($iFileMetaID)) {
             $iError = self::FILE_SAVE_INDEX_ERROR;
             $mReturn = 'Save file index failed.';
             break;
         }
         // 文件上传成功
         $mReturn = $o_aFileInfo;
     } while (false);
     return array($iError, $mReturn);
 }