function do_saveofficehour($teapro_id)
{
$db = init_db();
if( !empty($_FILES['teapro_officehour']['name']) )
{
$filepath = $_FILES['teapro_officehour']['tmp_name'];
$filename = $_FILES['teapro_officehour']['name'];
$filesize = $_FILES['teapro_officehour']['size'];
$filetype = $_FILES['teapro_officehour']['type'];
//判斷上傳檔案類型是否正確正確才能上傳
$doc_type_name = get_file_ext($filename);
$doc_filename = $teapro_id."_officehour".".".$doc_type_name;
$pass_doc_type2 = array('txt', 'doc', 'pdf');
if(in_array($doc_type_name, $pass_doc_type2))
{
//取出目前Server能夠上傳檔案的上限
$max_upload_filesize = ini_get('upload_max_filesize')*1024*1024;
if( $filesize > 0 AND $filesize <= $max_upload_filesize)
{
$filecontent = addslashes( fread( fopen( $filepath, "r"), $filesize));
$db->query("UPDATE mod_teacher_profile SET teapro_officehour_name = '$doc_filename', teapro_officehour = '$filecontent', teapro_officehour_type = '$filetype' WHERE teapro_id = $teapro_id");
}
}
}
}
將檔案從"資料庫"下載:
function show_officehour()
{
$db = init_db();
$teapro_id = db_escape($_GET['teapro_id']);
//$message = _('沒有這個檔案');
$results = $db->get_results( "SELECT teapro_officehour_name, teapro_officehour, teapro_officehour_type FROM mod_teacher_profile WHERE teapro_id = '$teapro_id'" );
if( isset($results) )
{
ob_start();
foreach($results as $row)
{
$filename = $row->teapro_officehour_name;
$ie = strnatcasecmp("MSIE" , $_SERVER['HTTP_USER_AGENT']);
if ($ie != -1) $filename = rawurlencode($filename);
Header("Content-type: $row->teapro_officehour_type");
Header("Content-Disposition: attachment; filename=\"$filename\"");
Header("Cache-Control: cache, must-revalidate");
echo $row->teapro_officehour;
break;
}
ob_flush();
}
}
將檔案上傳至"實體位置":
function do_savelessondate($tea_crs_id)
{
$db = init_db();
$file_field = empty($_FILES['tea_crs_file']) ? 'ch_tea_crs_file' : 'tea_crs_file' ;
if( !empty($_FILES[$file_field]['name']) )
{
$filepath = $_FILES[$file_field]['tmp_name'];
$filename = $_FILES[$file_field]['name'];
$filesize = $_FILES[$file_field]['size'];
$filetype = $_FILES[$file_field]['type'];
//判斷上傳檔案類型是否正確正確才能上傳
$doc_type_name = get_file_ext($filename);
$doc_filename = $tea_crs_id."".".".$doc_type_name;
$pass_doc_type2 = array('txt', 'doc', 'pdf','xls');
if(in_array($doc_type_name, $pass_doc_type2))
{
//取出目前Server能夠上傳檔案的上限
$max_upload_filesize = ini_get('upload_max_filesize')*1024*1024;
if( $filesize > 0 AND $filesize <= $max_upload_filesize)
{
if(! is_dir($this->upload_path)) // 如果 upload/teacher 不存在
mkdir($this->upload_path, 0700);
$file_path = $this->upload_path.$doc_filename;
move_uploaded_file(iconv("utf-8", "big5",$_FILES[$file_field]['tmp_name']), iconv("utf-8", "big5", $file_path));
$sql = "UPDATE mod_teacher_course SET tea_crs_lessonfile_name ='$filename', tea_crs_lessonfile_type='$doc_type_name' WHERE tea_crs_id = $tea_crs_id";
$db->query($sql);
}
}else{
return_notify('不支援該類檔案類型上傳');
}
}
}
將檔案從"實體位置"下載:
public static function download_xml()
{
$file_name="teacher_ex";
$Download_Filename = "./upload/teacher/".$file_name.".xml";
if(file_exists($Download_Filename) == false)
{
rename("./modules/teacher/".$file_name.".xml","./upload/teacher/".$file_name.".xml");
}
$Download_Filename = "./upload/teacher/".$file_name.".xml";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/vnd.ms-excel");
$user_agent = strtolower ($_SERVER["HTTP_USER_AGENT"]);
header( "Content-Disposition: attachment; filename=".$file_name.".xml");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($Download_Filename));
$handle = fopen($Download_Filename, "r");
$contents = fread($handle, filesize($Download_Filename));
print_r($contents);
fclose($handle);
flush();
}
記得要設定 $this->upload_path,範例如下:
function __construct() {
$this->upload_path = UPLOAD_PATH. '/activity';
}
沒有留言:
張貼留言