PHP文件上传,下载,Sql工具类!
生活随笔
收集整理的這篇文章主要介紹了
PHP文件上传,下载,Sql工具类!
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
PHP文件上傳,下載,Sql工具類! 對文件大小,文件類型 同名覆蓋 中文轉碼的操作,可直接使用
前臺 upload.html
<!DOCTYPE html> <html> <head><title>文件上傳</title> </head> <body> <form enctype="multipart/form-data" action="uploadProcess.php" method="post"><table><tr><td align="center" colspan="2"><font style="font-size: 40px; font-family: 華文彩云;" >文件上傳</font></td></tr><tr><td>請填寫用戶名:</td><td><input type="text" name="username"></td></tr><tr><td>介紹:</td><td><textarea name="intro" rows="10" cols="80"></textarea></td></tr><tr><td>請選擇你要上傳的文件</td><td><input type="file" name="myfile"></td></tr><tr><td><input type="submit" value="上傳文件" /></td></tr></table> </form> </body> </html>?
?
控制器?FileProcess.php
<?php require_once 'FileService.php'; $fileService = new FileService();if (!empty($_REQUEST['flag'])) {$flag = $_REQUEST['flag'];//上傳if ($flag == "upload") {$username = $_POST['username'];$intro = $_POST['intro'];$fileService -> Upload($username,$intro);}elseif ($flag == "down") {//接收要下載的文件名字$filepath = $_GET['filepath'];$filename = $_GET['filename'];$fileService = new FileService();$fileService->Download($filepath,$filename);} }?>?
后臺?FileService.php
?
<?php header("content_type:text/html;charset=utf-8"); require_once 'SqlHelper.php'; error_reporting(E_ALL & ~E_NOTICE);class FileService{//查詢所有文件信息 function fileInfo(){$sql = "select * from upload";$sqlHelper = new SqlHelper();$res = $sqlHelper->execute_dpl($sql);return $res;$res->free(); }/* *上傳文件 * 功能 * 限制文件大小/類型 * 防止不同用戶上傳同名圖片被覆蓋的問題 * 防止同一用戶上傳的文件名相同的問題 * 參數 * $username * $intro * *is_uploaded_file 上傳到tmp緩存 *move_uploaded_file 移動到目標文件 */ function Upload($username,$intro){/*********對文件類型進行限制**********///獲取文件的大小,限制上傳文件的大小10M$file_size = $_FILES['myfile']['size'];if ($file_size>10*1024*1024) {echo "<script>alert('上傳失敗,上傳的文件不能超過10M的文件');history.go(-1);</script>";//echo "上傳失敗,上傳的文件不能超過10M的文件!";exit();}//限制上傳文件類型/*$file_type = $_FILES['myfile']['type'];if ($file_type!='image/jpg' && $file_type!='image/pjpeg') {echo "上傳失敗,文件類型只能是jpg格式!";exit();}*///判斷文件是否上傳成功if (is_uploaded_file($_FILES['myfile']['tmp_name'])) {/***防止不同用戶上傳同名圖片被覆蓋的問題->給每個用戶創建一個文件夾*****///一般創建文件夾的時候根據id創建(username換成id,入參時帶入id),防止漢字亂碼//給每個用戶動態創建一個相應的文件夾$user_path = $_SERVER['DOCUMENT_ROOT']."Demo/File/UpDown/upload/".$username;//判斷該用戶是否已經有文件夾if (!file_exists($user_path)) {mkdir($user_path);}/****防止同一用戶上傳的文件名相同的問題->給每個文件名加上時間戳********///tmp里的文件名$file_name = $_FILES['myfile']['name'];//把緩存文件轉存到你希望的目錄$uploaded_file = $_FILES['myfile']['tmp_name'];//目標路徑=(目標目錄+用戶名)+當前時間+后綴(strpos()字符串首次出現的位置)$move_to_file = $user_path."/".time().rand(1,1000).substr($file_name, strpos($file_name, "."));//對中文路徑轉碼$move_to_file = iconv("utf-8", "gb2312", $move_to_file);/******************數據庫操作***********************///存入數據庫$uptime = date('Y-m-d H:i:s'); //獲取當前上傳時間$sql = "insert into upload (username,fname,fsize,uptime,fpath,intro) values ('$username','$file_name','$file_size','$uptime','$move_to_file','$intro')";$sqlHelper = new SqlHelper();$res = $sqlHelper->execute_dml($sql);//判斷是否將上傳的文件移動到目標位置(先判斷是否上傳成功,再判斷是否添加到數據庫)if (move_uploaded_file($uploaded_file, $move_to_file)) {//res=1表示添加上傳成功if ($res == 1) {echo "<script>alert('{$_FILES['myfile']['name']}文件上傳成功');window.location.href='down.php';</script>";}else{echo "<script>alert('文件上傳失敗');history.go(-1);</script>";}}else{echo "<script>alert('文件上傳失敗');history.go(-1);</script>";}}else{echo "<script>alert('文件上傳失敗');history.go(-1);</script>";} }/** * * 參數說明: * 下載文件 * $filepath 文件路徑 * $filename 文件名 * */ function Download($filepath,$filename){//對中文文件名進行轉碼$filename=iconv("UTF-8","GB2312",$filename); if(!file_exists($filepath)){ //檢查文件是否存在echo "<script>alert('該文件不存在!');history.go(-1);</script>";// echo "該文件不存在!";return;}$fp = fopen($filepath, 'r'); //打開文件$file_size = filesize($filepath); //計算文件大小if ($file_size>10*1024*1024) {echo "<script>window.alert('文件過大,您沒權限下載')</script>";return;}//HTTP頭部信息header("Content-type: application/octet-stream");header("Accept-Ranges: bytes");header("Accept-Length: ".$file_size);header("Content-Disposition: attachment; filename=".$filename);$buffer = 1024;//為了下載安全,做一個文件字節讀取計數器$file_count = 0;//判斷文件是否結束 feofwhile (!feof($fp) && ($file_size-$file_count > 0)) {$file_data = fread($fp, $buffer); //統計讀了多少字節$file_count+=$buffer;echo "$file_data"; //把數據會送給瀏覽器 }fclose($fp);}}?>?
?
?
?
工具類? SqlHelper.php
?
<?php/*** sql工具類(dml,dpl,dpl_arr,close_link)**1.創建MySqli對象*2.操作數據庫(發送sql)*3.處理結果*4.關閉資源**/class SqlHelper {private $link;private static $host = 'localhost';private static $user = 'root';private static $pwd = '';private static $db = 'test';public function __construct() {//初始化$this->link = new MYSQLi(self::$host,self::$user,self::$pwd,self::$db);if ($this->link->connect_error){die("數據庫連接失敗".$this->link->connect_error);}$this->link->query("set names utf8");}/*** dpl操作* @param unknown $sql*/public function execute_dpl($sql){$res = $this->link->query($sql) or die("操作dpl失敗".$this->link->error);return $res;}/*** dpl操作* @param $sql* @return arr* 把結果放在數組里。這樣資源可以隨時關閉,返回一個數組*/public function execute_dpl_arr($sql){$arr = array();$res = $this->link->query($sql) or die("操作dpl_arr失敗".$this->link->error);//把$res=>$arr,把結果集內容轉移到一個數組中while ($row = $res->fetch_assoc()){$arr[] = $row;}//這里可以把資源立即關閉$res->free();return $arr;}/*** dml操作 update/delete/insert* @param unknown $sql*/public function execute_dml($sql){$res = $this->link->query($sql) or die("操作dml失敗".$this->link->error);if (!$res){return 0; //失敗}else {if ($this->link->affected_rows>0){return 1; //成功}else {return 2; //沒有行受到影響 }}$res->free();}//關閉鏈接public function close_link(){if (!empty($this->link)){$this->link->close();}}}?
轉載于:https://www.cnblogs.com/zxf100/p/6736071.html
總結
以上是生活随笔為你收集整理的PHP文件上传,下载,Sql工具类!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python scrapy 抓取脚本之家
- 下一篇: Swift的控制转移语句-- fallt