<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>php檔案上傳</title> </head> <body> <form action="uploadsir.php" method="post" enctype="multipart/form-data"><input name="filedata" type="file" id="filedata" /> <input type="submit" name="submit" value="上傳" /></form> <?php if(!$_post)die(); $state=uploadfile('filedata'); if($state['err']){ die('<script>alert("上傳出錯:'.$state['msg'].'");history.go(-1);</script>'); } echo'<object type="application/x-shockwave-flash" data="template/images/copy.swf?u='.weburl.$state['msg'].'" width="100" height="40"> <param name="movie" value="copy.swf?u='.weburl.$state['msg'].'" />'; function uploadfile($inputname) { $immediate=$_get['immediate']; $attachdir='../pictures';//上傳檔案儲存路徑,結尾不要帶/ $urldir="../pictures"; $dirtype=2;//1:按天存入目錄 2:按月存入目錄 3:按副檔名存目錄 建議使用按天存 $maxattachsize=2097152;//最大上傳大小,預設是2m $upext='txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid,jar,jad,exe,html,htm,css,js,doc';//上傳副檔名 $err = ""; $msg = ""; $upfile=$_files[$inputname]; if(!empty($upfile['error'])) { switch($upfile['error']) { case '1': $err = '檔案大小超過了php.ini定義的upload_max_filesize值'; break; case '2': $err = '檔案大小超過了html定義的max_file_size值'; break; case '3': $err = '檔案上傳不完全'; break; case '4': $err = '無檔案上傳'; break; case '6': $err = '缺少臨時檔案夾'; break; case '7': $err = '寫檔案失敗'; break; case '8': $err = '上傳被其它擴充中斷'; break; case '999': default: $err = '無有效錯誤碼'; } } elseif(empty($upfile['tmp_name']) || $upfile['tmp_name'] == 'none')$err = '無檔案上傳'; else { $temppath=$upfile['tmp_name']; $fileinfo=pathinfo($upfile['name']); $extension=$fileinfo['extension']; if(preg_match('/'.str_replace(',','|',$upext).'/i',$extension)) { $filesize=filesize($temppath); if($filesize > $maxattachsize)$err='檔案大小超過'.$maxattachsize.'位元組'; else { switch($dirtype) { case 1: $attach_subdir = 'day_'.date('ymd'); break; case 2: $attach_subdir = 'month_'.date('ym'); break; case 3: $attach_subdir = 'ext_'.$extension; break; } $attach_dir = $attachdir.'/'.$attach_subdir; if(!is_dir($attach_dir)) { @mkdir($attach_dir, 0777); @fclose(fopen($attach_dir.'/index.htm', 'w')); } php_version < '4.2.0' && mt_srand((double)microtime() * 1000000); $filename=date("ymdhis").mt_rand(1000,9999).'.'.$extension; $target = $urldir.'/'.$attach_subdir.'/'.$filename; move_uploaded_file($upfile['tmp_name'],$target); if($immediate=='1')$target='!'.$target; $msg=str_replace('../',"",$target); } } else $err='上傳副檔名必需為:'.$upext;
@unlink($temppath); } return array('err'=>$err,'msg'=>$msg); } ?> </body> </html> |