Template::OnUpload PHP Method

OnUpload() public static method

Call this to upload a template file. Note: Template::UpdateStatus() will be called automatically for you if this is successful.
public static OnUpload ( $f_fileVarName, string $p_baseUpload, string $p_desiredName = null, string $p_charset = null ) : mixed
$p_baseUpload string Directory path to add to the base template directory.
$p_desiredName string Desired name of the file.
$p_charset string Desired character set of the file.
return mixed TRUE on success, PEAR_Error on failure.
    public static function OnUpload($f_fileVarName, $p_baseUpload, $p_desiredName = null, $p_charset = null)
    {
        global $Campsite;
        $p_baseUpload = $Campsite['TEMPLATE_DIRECTORY'] . $p_baseUpload;
        if (!isset($_FILES[$f_fileVarName]) || !isset($_FILES[$f_fileVarName]['name'])) {
            return new PEAR_Error("Invalid parameters given to Template::OnUpload()");
        }
        if (is_null($p_desiredName)) {
            $fileName = $_FILES[$f_fileVarName]['name'];
        } else {
            $fileName = $p_desiredName;
        }
        // remove existing file if one exists
        $newname = $p_baseUpload . DIR_SEP . $fileName;
        if (file_exists($newname) && !is_dir($newname)) {
            if (!unlink($newname)) {
                return new PEAR_Error(camp_get_error_message(CAMP_ERROR_DELETE_FILE, $newname), CAMP_ERROR_DELETE_FILE);
            }
        }
        $fType = $_FILES[$f_fileVarName]['type'];
        if (!is_null($p_charset)) {
            // original file, destination file, file type, file charset
            $success = self::ChangeCharset($_FILES[$f_fileVarName]['tmp_name'], $newname, $fType, $p_charset);
        } else {
            $success = move_uploaded_file($_FILES[$f_fileVarName]['tmp_name'], $newname);
            if (!$success) {
                return new PEAR_Error(camp_get_error_message(CAMP_ERROR_CREATE_FILE, $newname), CAMP_ERROR_CREATE_FILE);
            }
        }
        if ($success) {
            self::UpdateStatus();
        }
        return $success;
    }

Usage Example

Example #1
0
$oldFilePath = Template::GetFullPath($f_path, $f_old_name);
$oldMimeType = (function_exists('mime_content_type')) ? mime_content_type($oldFilePath) :
							camp_mime_content_type($oldFilePath);
$oldRelativeFilePath = (!empty($f_path)) ? ltrim($f_path.DIR_SEP.$f_old_name, '/') : $f_old_name;
$newMimeType = $_FILES['f_file']['type'];
$equivalentTextTypes = array("text/plain", "text/html", "application/x-php", "application/octet-stream", "application/javascript", "text/x-c", "text/css" , "text/x-php", "application/x-httpd-php", "text/x-c++");
$matched = false;
if (in_array($oldMimeType, $equivalentTextTypes) && in_array($newMimeType, $equivalentTextTypes)) {
	$matched = true;
}
if (!$matched && ($oldMimeType != $newMimeType)) {
	camp_html_add_msg(getGS('You can only replace a file with a file of the same type.  The original file is of type "$1", and the file you uploaded was of type "$2".', $oldMimeType, $newMimeType));
	camp_html_goto_page($backLink);
}

// Move the new file it its place
$success = Template::OnUpload("f_file", $f_path, $f_old_name);
if ($success) {
        // Clear compiled template
        require_once($GLOBALS['g_campsiteDir']."/template_engine/classes/CampTemplate.php");
        CampTemplate::singleton()->clear_compiled_tpl($oldRelativeFilePath);

	camp_html_add_msg(getGS('File "$1" replaced.', $f_old_name), "ok");
} else {
	camp_html_add_msg(getGS("Unable to save the file '$1' to the path '$2'.", $fileName, $f_path) . " "
			. getGS("Please check if the user '$1' has permission to write in this directory.", $Campsite['APACHE_USER']));
}
camp_html_goto_page($backLink);

?>