<?php class FileOperSer{ //文件下载 function fileDown($file_name){ //iconv()把编码改变 //原因 php文件函数,比较古老,需要对中文转码 gb2312 $file_name=iconv("utf-8","gb2312",$file_name); //绝对路径 $file_path=$_SERVER['DOCUMENT_ROOT'].$file_name; //打开图片 if(!file_exists($file_path)){ echo "没有这个图片"; return; } $fp=fopen($file_path,"r"); //获取图片大小 $file_size=filesize($file_path); if($file_size>10*1024*1024){ echo "过大"; return; } //返回的文件 header("Content-type: application/octet-stream"); //按照字节大小返回 header("Accept-Ranges: bytes"); //返回文件大小 header("Accept-Length: $file_size"); //客户端的弹出对话框,对应的文件名 header("Content-Disposition: attachment; filename=".$file_name); //向客户端回送数据 $buffer=1024; $file_count=0; while (!feof($fp) && ($file_size-$file_count)>0){ $file_data=fread($fp, $buffer); //统计字节 $file_count+=$buffer; //把部分数据回送给浏览器; echo $file_data; } //关闭文件 fclose($fp); } //文件上传 public function fileUp($username,$fileintro,$fileService){ //获取文件类型 $fileType=$_FILES['myfile']['type']; if($fileType!="image/jpg" && $fileType!="image/jpeg"){ echo "文件只能上传jpg图片类型"; //header("refresh:3;upload.php"); exit(); } //获取文件的大小 $file_size=$_FILES['myfile']['size']; if($file_size>2*1024*1024){ echo "不能上传大于2M的文件"; exit(); } if(is_uploaded_file($_FILES['myfile']['tmp_name'])){ //把路径赋值到变量中 $upload_file=$_FILES['myfile']['tmp_name']; $user_path="/upfile/up/".$username; $user_path=iconv("utf-8","gb2312", $user_path); if(!file_exists($_SERVER["DOCUMENT_ROOT"].$user_path)){ mkdir($_SERVER["DOCUMENT_ROOT"].$user_path); } //把中文文件名进行编码 //$fileName=iconv("utf-8","gb2312",$_FILES['myfile']['name']); //随机产生 $file_true_pos=$_FILES['myfile']['name']; $move_to=$user_path."/".date("YmdHis",time()).rand(1,100). substr($file_true_pos,strrpos($file_true_pos,".")); $move_to_file= $_SERVER['DOCUMENT_ROOT'].$move_to; // 改变路径,如果成功传入信息 if (move_uploaded_file($upload_file,$move_to_file)){ $move_to= iconv("gb2312", "utf-8", $move_to); if($fileService->addFiledb($move_to, $username, $fileintro)==1){ header("Location:ok.php"); exit(); }else{ header("Location:error.php"); exit(); } }else{ //header("refresh:3;upload.php"); echo "上传失败"; exit(); } }else{ // header("refresh:3;upload.php"); echo "上传失败"; exit(); } } } ?>