Exemple #1
0
 /**
  * Validate file upload
  * @param String $sFormName
  * @param int $iMaxUploadSize in bytes
  * @param String $sAllowedExtension, comma separated supported file
  * @return <type>
  */
 public static function validateFileUpload($sFormName, $aiMaxUploadSize = null, $sAllowedExtension = null)
 {
     $iMaxUploadSize = $aiMaxUploadSize;
     if (is_null($iMaxUploadSize) && (FlexiConfig::$sFramework == "modx" || FlexiConfig::$sFramework == "modx2")) {
         global $modx;
         $iMaxUploadSize = $modx->config["upload_maxsize"];
     }
     $bUploadErr = false;
     $iSize = self::getUploadedFileSize($sFormName);
     if (!empty($iMaxUploadSize) && $iSize > $iMaxUploadSize) {
         $sNotice = flexiT("File Upload exceed permitted size", "first") . ": " . $iSize . " / " . $iMaxUploadSize;
         return array("status" => false, "msg" => $sNotice);
     }
     $sExtension = strtolower(trim(FlexiFileUtil::getUploadedFileExtension($sFormName)));
     if (!empty($sAllowedExtension)) {
         $sAllowed = "";
         switch ($sAllowedExtension) {
             case "image":
             case "media":
                 $sAllowed = "jpg,jpeg,png,gif";
             case "video":
             case "media":
                 $sAllowed .= "mp4,wma,avi,mpeg,mov,flv";
             case "sound":
             case "media":
                 $sAllowed .= "mp3,wav";
         }
         $sAllowed = empty($sAllowed) ? $sAllowedExtension : $sAllowed;
         $aAllowed = explode(",", $sAllowed);
         if (!in_array($sExtension, $aAllowed)) {
             $sNotice = flexiT("Only of type", "first") . ": jpeg or png " . flexiT("are permitted") . ", " . flexiT("you have uploaded") . ": " . $sExtension;
             return array("status" => false, "msg" => $sNotice);
         }
         //if not in allowed extension
     }
     //allowed extension is set
     return array("status" => true, "msg" => "");
 }
Exemple #2
0
 public static function processUpload($sFieldName, $sSavePath, $aExtension, $sSaveName, $aiMaxUpload = null, $bReplace = true, $sOldPath = null)
 {
     $iMaxUpload = self::getMaxUploadFileSize($aiMaxUpload);
     if (FlexiFileUtil::getIsUploaded($sFieldName)) {
         $iSize = FlexiFileUtil::getUploadedFileSize($sFieldName);
         if ($iSize > $iMaxUpload) {
             $sNotice = flexiT("File Upload exceed permitted size", "first") . ": " . $iSize . " / " . $iMaxUpload;
             throw new FlexiException($sNotice, ERROR_FILESIZE);
         }
         $sExtension = strtolower(trim(FlexiFileUtil::getUploadedFileExtension($sFieldName)));
         if (!in_array($sExtension, $aExtension)) {
             $sNotice = flexiT("File type not permitted", "first") . ", " . flexiT("you have uploaded") . ": " . $sExtension;
             throw new FlexiException($sNotice, ERROR_FILETYPE);
         }
         //replace photo if already exists
         if ($bReplace && !empty($sOldPath)) {
             //fix windows path issue
             $sOldFile = substr($sOldPath, 0, 1) == "/" || substr($sOldPath, 1, 2) == ":\\" ? $sOldPath : FlexiFileUtil::getBasePath() . "/" . $sOldPath;
             @unlink($sOldFile);
         }
         $aStatus = FlexiFileUtil::doUploadFile($sFieldName, $sSavePath, $sSaveName . ".");
         //
         return $aStatus;
     }
     //is upload
     return false;
 }