controllers/UploadController.php
/**
     * Upload file on system
     * 
     * @param array $formParams
     */
Upload file on system

@param array $formParams
Upload file on system @param array $formParams
public function uploadFile(array $formParams) {
        $category = $formParams['category_id'];
        $language = $formParams['language_id'];
        if (!empty($_FILES)) {
            if ($_FILES[self::FILE_UPLOAD_FILED_NAME]["error"] > 0) {
                Logger::getLogger()->LogFatal('File upload failed, Errors found in $FILES array');
                Response::sendResponse(Constants::FAILURE_RESPONSE, Messages::ERROR_SOMETHING_WENT_WRONG);
            } else {
                $originalFileName = $_FILES[self::FILE_UPLOAD_FILED_NAME]["name"];
                $nameParts = explode(".", $originalFileName);
                $extension = end($nameParts);
                if (in_array($extension, $this->allowedExts)) {
                    $newFileName = Utils::getStoredFileName($originalFileName);
                    $uploadFileDir = Configuration::get(Configuration::CODE_BASE_DIR).$language.'/'.$category;
                    if ($this->checkAndCreateDir($uploadFileDir)) {
                        $uploadFileLocation = $uploadFileDir.'/'.$newFileName;
                        if (!move_uploaded_file($_FILES[self::FILE_UPLOAD_FILED_NAME]["tmp_name"], $uploadFileLocation)) {
                            Logger::getLogger()->LogFatal("File upload failed while trying move_uploaded_file()");
                            Response::sendResponse(Constants::FAILURE_RESPONSE, Messages::ERROR_SOMETHING_WENT_WRONG);
                        } else {
                            $authUserData = Session::get(Session::SESS_USER_DETAILS);
                            $authUserId = $authUserData[Users_DBTable::USER_ID];
                            $formParams['actual_file_name'] = $originalFileName;
                            $formParams['stored_file_name'] = $newFileName;
                            $formParams['created_by'] = $authUserId;
                            if ($this->getModel()->insertProgramDescription($formParams)) {
                                Response::sendResponse(Constants::SUCCESS_RESPONSE, Messages::SUCCESS_UPDATE);
                            } else {
                                Response::sendResponse(Constants::FAILURE_RESPONSE, Messages::ERROR_OPERATION_FAILED);
                            }
                        }
                    } else {
                        Logger::getLogger()->LogError("Directory < $uploadFileDir > Doesn't Exist or Not Writable");
                        Response::sendResponse(Constants::FAILURE_RESPONSE, Messages::ERROR_OPERATION_FAILED);
                    }
                } else {
                    Logger::getLogger()->LogError(Messages::UPLOAD_INVALID_FILE_TYPE);
                    Response::sendResponse(Constants::FAILURE_RESPONSE, Messages::UPLOAD_INVALID_FILE_TYPE);
                }
            }
        }
    }
public function uploadFile ( array $ formParams ) { $ category = $ formParams [ 'category_id' ] ; $ language = $ formParams [ 'language_id' ] ; if ( ! empty ( $ _FILES ) ) { if ( $ _FILES [ self :: FILE_UPLOAD_FILED_NAME ] [ " error " ] > 0 ) { Logger :: getLogger ( ) -> LogFatal ( 'File upload failed, Errors found in $FILES array' ) ; Response :: sendResponse ( Constants :: FAILURE_RESPONSE , Messages :: ERROR_SOMETHING_WENT_WRONG ) ; } else { $ originalFileName = $ _FILES [ self :: FILE_UPLOAD_FILED_NAME ] [ " name " ] ; $ nameParts = explode ( " . " , $ originalFileName ) ; $ extension = end ( $ nameParts ) ; if ( in_array ( $ extension , $ this -> allowedExts ) ) { $ newFileName = Utils :: getStoredFileName ( $ originalFileName ) ; $ uploadFileDir = Configuration :: get ( Configuration :: CODE_BASE_DIR ) . $ language . '/' . $ category ; if ( $ this -> checkAndCreateDir ( $ uploadFileDir ) ) { $ uploadFileLocation = $ uploadFileDir . '/' . $ newFileName ; if ( ! move_uploaded_file ( $ _FILES [ self :: FILE_UPLOAD_FILED_NAME ] [ " tmp_name " ] , $ uploadFileLocation ) ) { Logger :: getLogger ( ) -> LogFatal ( " File upload failed while trying move_uploaded_file() " ) ; Response :: sendResponse ( Constants :: FAILURE_RESPONSE , Messages :: ERROR_SOMETHING_WENT_WRONG ) ; } else { $ authUserData = Session :: get ( Session :: SESS_USER_DETAILS ) ; $ authUserId = $ authUserData [ Users_DBTable :: USER_ID ] ; $ formParams [ 'actual_file_name' ] = $ originalFileName ; $ formParams [ 'stored_file_name' ] = $ newFileName ; $ formParams [ 'created_by' ] = $ authUserId ; if ( $ this -> getModel ( ) -> insertProgramDescription ( $ formParams ) ) { Response :: sendResponse ( Constants :: SUCCESS_RESPONSE , Messages :: SUCCESS_UPDATE ) ; } else { Response :: sendResponse ( Constants :: FAILURE_RESPONSE , Messages :: ERROR_OPERATION_FAILED ) ; } } } else { Logger :: getLogger ( ) -> LogError ( " Directory <  $ uploadFileDir  > Doesn't Exist or Not Writable " ) ; Response :: sendResponse ( Constants :: FAILURE_RESPONSE , Messages :: ERROR_OPERATION_FAILED ) ; } } else { Logger :: getLogger ( ) -> LogError ( Messages :: UPLOAD_INVALID_FILE_TYPE ) ; Response :: sendResponse ( Constants :: FAILURE_RESPONSE , Messages :: UPLOAD_INVALID_FILE_TYPE ) ; } } } }
Upload file on system
Upload file on system