ceag統一使用檔案上傳的方式,把所有的檔案集中於files目錄,
每年新建一個資料夾(以西元年為資料夾名稱)
資料庫則建立於files,具有底下欄位
news_id p_id file_name file_server timenow ip userid db_name data_id data_now file_kind userid2
上傳:
function FILE_UPLOAD( $userfile , $up_filekind="all" , $db_name="",$data_id="",$userfile_txt=""){
if($_FILES[$userfile]['name']==""){ return false ; }
include __DIR__."/conn_kles_x975dfa.php";
$uploadfile = $UP_FILE_DATA . $_FILES[$userfile]['name'] ;
if (move_uploaded_file($_FILES[$userfile]['tmp_name'], $uploadfile)) {
$File_Extension = explode(".", $uploadfile); //先用.分開字串
$File_Extension_NAME = $File_Extension[0]; //取得第一個值,即不含副檔名
$File_Extension = $File_Extension[count($File_Extension)-1]; //取得最後一個值(副檔名
$File_Extension = strtolower($File_Extension); //轉小寫
if($File_Extension=="jpeg") $File_Extension="jpg";
//檢查副檔名,若不符合要求,中斷上傳作業
if($up_filekind =="all"){ //檢查副檔名,允許所有資料庫中符合的檔名
$sql = "Select * From data_filekind Where filekind='".$File_Extension."' limit 1";
$rows = mysql_query($sql);
$num = mysql_num_rows($rows) ;
if($num <> 1){
echo "";
exit ;
}
}else{//檢查副檔名,只允許指定的副檔名上傳
if($up_filekind <> $File_Extension){ //只檢查指定的副檔名與上傳檔名
echo "";
exit ;
}
}//檢查副檔名
//將檔案重新命名
$data_name ="f". date("y") . date("m"). date("d"). "_" . date("H"). date("i"). date("s") . GET_RAND() ;
$data_name4 = $data_name ; //把正式檔名存在data_name4(不含附檔名)
$data_name = $data_name .".". $File_Extension ;
$data_name =strtolower($data_name); //確定轉小寫
$path = $UP_FILE_DATA . date("Y") ;
if ( ! is_dir($path)) // 建立此使用者目錄
mkdir($path);
$all_data_name = $path ."/".$data_name ; // 將$data_name轉為絕對路徑
rename( $uploadfile , $all_data_name ); // 用絕對路徑,重新命名。
chmod($all_data_name , 0777);//設權限為777
//將資料寫入檔案資料表files
$p_id = $p_id = uniqid() ; //唯一碼+序號
$userid = $sess_userid ;
if(empty($sess_userid)){ $userid = $sess_school ; }
$file_name9 =$_FILES[$userfile]['name'] ;
if($userfile_txt<>""){$file_name9=$userfile_txt ;}
$file_name9 = str_replace(".".$File_Extension,"",$file_name9) ;//把副檔名移除
$file_name9 = str_replace(".".strtoupper($File_Extension),"",$file_name9) ;//把副檔名移除(大寫)
$sql_add = "INSERT INTO `$DB_FILE`
(`p_id`,`userid`, `ip`, `timenow`,`file_name`,`file_server`,`file_kind`,`db_name`,`data_id`) VALUES ('$p_id','$userid' , '$ip', '$timenow','$file_name9','$data_name','$File_Extension','$db_name','$data_id');";
mysql_query($sql_add) || die("資料庫有誤,請連絡管理員。2");
////////////////////////////////////////////////////////////////////////////////////
//若為jpg,製成縮圖 (省略.....)
////////////////////////////////////////////////////////
$up_p_id = $p_id ; // 上傳檔案的原始檔名。
return $up_p_id ;
} // if(move_uploaded_fiel....)
} //End Function
function FILE_DEL($file_id,$db,$p_id,$data_files="data_files"){
//global $sess_userid;
require("conn_kles_x975dfa.php");
$sql_news = "select * from `$db` Where `p_id`='$p_id' AND `data_now`='1' Limit 1";
$sql = "select * from `$DB_FILE` Where `p_id`= '$file_id' AND `data_now`='1' Limit 1";
if(ASK_DATA_SQL_NUM($sql)==0 || ASK_DATA_SQL_NUM($sql_news)==0){
echo "";
exit ;
}
//權限確認
if(ASK_DATA_SQL($sql,userid)<>$sess_userid AND $sess_level < 90){
echo "";
exit ;
}
$sql_del = "update `$DB_FILE` SET `data_now` = '0',
`userid2` = $sess_userid Where `p_id`= '$file_id' limit 1";
mysql_query($sql_del) || die("資料庫有誤,請連絡管理員。「檔案資料庫」");
//取消data_files裡的檔案名稱資料
$data_files0 = ASK_DATA_SQL($sql_news,$data_files);
$file_id2 = $file_id.",";
$data_files0 = str_replace($file_id2,"",$data_files0);
$data_files0 = str_replace($file_id,"",$data_files0);
if ( substr($data_files0, -1) == "," ) $data_files0 = substr($data_files0, 0, strlen($data_files0)-1) ;
if ( substr($data_files0, 0,1) == "," ) $data_files0 = substr($data_files0, 1, strlen($data_files0)-1) ;
$sql_up2 = "update `$db` SET `$data_files` = '$data_files0',
`userid2` = $sess_userid Where `p_id`= '$p_id' limit 1";
mysql_query($sql_up2) || die($sql_up2 ."資料庫有誤,請連絡管理。747541");
/*
echo "";
exit ;
*/
return "true";
}//end function
///////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
function FILE_FIX($file_id,$file_txt,$data_files="data_files"){
if($file_txt==""){
echo ""; exit ;
}
require("conn_kles_x975dfa.php");
$sql = "select * from `$DB_FILE` Where `p_id`= '$file_id' AND `data_now`='1' Limit 1";
if(ASK_DATA_SQL_NUM($sql)==0 ){
echo "";
exit ;
}
//權限確認
if(ASK_DATA_SQL($sql,userid)<>$sess_userid AND $sess_level < 90){
echo "";
exit ;
}
$file_kind = ".".ASK_DATA_SQL($sql,file_kind);
$file_txt = str_replace($file_kind,"",$file_txt); ;
$sql_fix = "update `$DB_FILE` SET `file_name` = '$file_txt',`userid2` = $sess_userid Where `p_id`= '$file_id' limit 1";
mysql_query($sql_fix) || die("資料庫有誤,請連絡管理員。「檔案資料庫」");
return "true";
}//end function
//////////////////////////////////////////////////////////////
function FILE_UP($db,$p_id,$up_file,$up_file_txt,$file_kind="all",$data_files="data_files"){
require("config.php");
$sql = "select * from `$db` where `p_id` ='$p_id' AND `data_now`=1 limit 1";
if(ASK_DATA_SQL_NUM($sql)==0 ){
echo "";
exit ;
}
/*暫不檢查。
if(ASK_DATA_SQL($sql,userid)<>$sess_userid AND $sess_level < 90){
die("資料有誤84568");
echo "";
exit ;
}
*/
//開始上傳
$db_news_id = ASK_DATA_SQL($sql,news_id) ;
$aaaxx = FILE_UPLOAD( $up_file ,$file_kind,$db,$db_news_id,$up_file_txt);
//$aaaxx = $up_p_id ; //該FILE_UPLOAD變數為傳回之值
if($aaaxx<>""){ //如果有值,就是有更換上傳檔案,沒有就是沒有。
$data_file0 = trim(ASK_DATA_SQL($sql,$data_files) );
$data_file0 = trim($data_file0,"," );
//去除前後逗號
// if ( substr($data_file0, -1) == "," ) $data_file0 = substr($data_file0, 0, strlen($data_file0)-1) ;
// if ( substr($data_file0, 0,1) == "," ) $data_file0 = substr($data_file0, 1, strlen($data_file0)-1) ;
$data_file0 = $data_file0 .",". $aaaxx ;
$data_file0 = trim($data_file0,"," );
$sql_up4567 = "UPDATE `$db` SET `$data_files` = '$data_file0' WHERE `p_id` = '$p_id' LIMIT 1 ; ";
//echo $sql_up4567 ;
mysql_query($sql_up4567) || die("資料庫有誤,請連絡管理員。7010." .mysql_error() ."");
}
}
沒有留言:
張貼留言