PHP封裝成類(檔案上傳)
uploadfile.class.php
<?php /** * Created by PhpStorm. * User: huang * Date: 2017/7/19 * Time: 23:20 */ header("content-type:text/html;charset=utf-8"); $new = new upload(); class upload{ protected $fileName; protected $fileInfo; protected $maxSize; protected $allowExt; protected $allowMime; protected $uploadPath; protected $imgFlag; private $Error; private $Ext; public function __construct($fileName='myfile', $uploadPath='uploads', $maxSize = '2097152', $allowExt = array('jpg', 'jpeg', 'png', 'git', 'wbmp'), $allowMime =array(' image/png','image/jpeg','image/gif'),$imgFlag = true){ $this->fileName = $fileName; $this->fileInfo = $_FILES[$this->fileName]; $this->maxSize = $maxSize; $this->allowExt = $allowExt; $this->allowMime = $allowMime; $this->uploadPath = $uploadPath; $this->imgFlag = $imgFlag; } //產生唯一字串 protected function getUniname(){ return md5(uniqid(microtime(true),true)); } //上傳檔案 public function upload(){ if($this->checkError()&&$this->checkSize()&&$this->checkExt()&&$this->imgTure()&&$this->checkMime()&&$this->checkHTTPpost()){ $this->checkuploadPath(); $this->uniName = $this->getUniname(); $this->destination = $this->uploadPath.'/'.$this->uniName.'.'.$this->Ext; if(@move_uploaded_file($this->fileInfo['tmp_name'] , $this->destination)){ return $this->destination; }else{ $this->Error = '移動檔案失敗'; $this->showError(); } }else{ $this->showError(); } } //檢測目錄是否存在,建立目錄 protected function checkuploadPath(){ if(!file_exists($this->uploadPath)){ mkdir($this->uploadPath,0777,true); } } //檢測檔案大小 public function checkSize(){ if($this->fileInfo['size'] > $this->maxSize){ $this->Error = $this->fileInfo['name'].'檔案過大'; return false; } return true; } //檢測是否通過HTTP、POST方式上傳 protected function checkHTTPpost(){ if(!is_uploaded_file($this->fileInfo['tmp_name'])){ $this->Error = '不是通過HTTP、POST方式上傳的'; return false; } return true; } //顯示錯誤 protected function showError(){ exit('<span style="color:red">'.$this->Error.'</span>'); } //檢測檔案拓展名 public function checkExt(){ $this->Ext = strtolower(pathinfo($this->fileInfo['name'],PATHINFO_EXTENSION)); if(!in_array($this->Ext,$this->allowExt)){ $this->Error = '不允許的拓展名'; return false; } return true; } //是否檢測圖片型別 protected function imgTure(){ if($this->imgFlag){ if(
[email protected]($this->fileInfo['tmp_name'])){ $this->Error = '不是真實圖片'; return false; } return true; } } //檢測檔案格式 public function checkMime(){ if([email protected]_array($this->fileInfo['type'],$this->allowMime)){ $this->Error = '不允許的圖片型別'; return false; } return true; } //檢測是否有錯 public function checkError(){ if(!is_null($this->fileInfo['error'])){ if($this->fileInfo['error'] > 0){ switch ($this->fileInfo['error']) { case 1: $this->Error = '上傳檔案超過了配置檔案中upload_max_filesize選項的值'; break; case 2: $this->Error = '超過表單MAX_FILE_SIZE限制的大小'; break; case 3: $this->Error = '檔案上傳不完整'; break; case 4: $this->Error = '沒有選擇上傳檔案'; break; case 6: $this->Error = '沒有找到臨時目錄'; break; case 7: $this->Error = '檔案不可寫'; break; case 8: $this->Error = '由於PHP的擴充套件程式中斷檔案上傳'; break; } return false; }else{ return true; } }else{ $this->Error = '檔案上傳出錯'; return false; } } } ?>
相關推薦
PHP封裝成類(檔案上傳)
uploadfile.class.php <?php /** * Created by PhpStorm. * User: huang * Date: 2017/7/19 * Time: 23:20 */ header("content-type:text
day20JavaWeb(檔案上傳)
上傳不能使用BaseServlet 因為無法獲取引數了getParameter 方法不能用 檔案上傳概述 1、檔案上傳的作用 例如網路硬碟,就是用來上傳下載檔案的 2、檔案上傳也對面的要求 1、必須使用表單,而不能是超連結 2、表單的method必須是POST,而不能是GET
SpringMVC(14) - spring的多部件(檔案上傳)支援
參考:https://docs.spring.io/spring/docs/4.3.20.RELEASE/spring-framework-reference/htmlsingle/#mvc-multipart 1. 簡介 Spring的內建多部件支援處理Web應用程式中的檔案
spring的多部件(檔案上傳)支援
1. 簡介 Spring的內建多部件支援處理Web應用程式中的檔案上傳。可以使用org.springframework.web.multipart包中定義的可插入的MultipartResolver物件啟用此多部件支援。 Spring提供了一個用於Commons Fi
使用FormData對包含檔案型別的form表單進行非同步提交(檔案上傳)並配置回撥
直接使用form表單的submit提交按鈕進行表單提交到action,這個是非非同步的,不但在action返回時需要重新整理頁面或跳轉至另外一個頁面,也不能配置回撥函式對返回資料進行某些處理。 其次,如果使用表單的serialize()方法進行ajax提交,則只能傳遞簡單
SpringMVC_day02_8(檔案上傳)
六. 檔案上傳1. MultipartResovler: 檔案上傳解析器. 1.1 將檔案以檔案流的形式包裝到請求中,然後通過請求上傳到 伺服器 1.2 作用:把請求物件中檔案內容解析成MultipartFile物件. 1.3 一個檔案域(<inp
PHP大檔案上傳(切片上傳)
由於專案需要,經常要上傳幾百兆或者幾個G的檔案。考慮到檔案過大,直接上傳的話會超出PHP設定的表單提交限制大小,同時會佔用較多的系統資源。於是考慮將檔案進行切片,然後將切片後的檔案統一上傳至檔案目錄,待全部上傳成功之後再將其合併成一個檔案,同時後臺md5驗證是否上傳成功。
Java框架-SpringMVC的應用(json資料互動、控制器方法返回值、檔案上傳)
1. 搭建SpringMVC開發環境 1.1 建立專案,新增依賴 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" x
Java進階學習第十三天(檔案上傳與下載、JavaMail郵件開發)
一、檔案上傳與下載 1、檔案上傳 案例:登錄檔單/儲存商品等相關模組! 頭像 / 商品圖片(資料庫:儲存圖片路徑 / 圖片儲存到伺服器中指定的目錄) 2、檔案上傳要點 ① 表單提交方式:post ② 表單中有檔案上傳的表單項: <input type=”file” />
php進階——02 多檔案上傳
前言 使用MVC的思想去封裝一個多檔案上傳類,入口檔案為index.php,檢視檔案有single和group2個html檔案,controller有upload.class.php。 index.php // 1.定義根目錄常量FILEROOT
Android網路框架Retrofit2使用封裝:Get/Post/檔案上傳/下載
背景 Android開發中的網路框架經過多年的發展,目前比較主流的就是Retrofit了,Retrofit2版本出現也有幾年了,為了方便使用,特封裝了一些關於Retrofit2的程式碼,分享給大家。 框架主要包括: Get請求 Post請求 檔案上傳 檔案下載
Java實現的SFTP(檔案上傳詳解篇)
JSch是Java Secure Channel的縮寫。JSch是一個SSH2的純Java實現。它允許你連線到一個SSH伺服器,並且可以使用埠轉發,X11轉發,檔案傳輸等,當然你也可以整合它的功能到你自己的應用程式。 本文只介紹如何使用JSch實現的SFTP功能
Java框架(十五)之springMVC(檔案上傳、攔截器)
一、jackson @RequestBody/ @ResponseBody處理Json資料 作用: @RequestBody註解用於讀取http請求的內容(字串),通過springmvc提供的HttpMessageConverter介面將讀到的內容轉換為json
requests 進階用法學習(檔案上傳、cookies設定、代理設定)
一、檔案上傳 1、模擬網站提交檔案 提交此圖片,圖片名稱:timg.jpg import requests files={ 'file':open('timg.jpg','rb') } response=requests.post('http://httpbin.or
dokuwiki學習(九)——編輯媒體檔案(覆蓋上傳)
在刪除按鈕旁邊,有一個上傳新版本。dokuwiki提供了類似版本控制的功能。我們來試一下。 我們對excel檔案內容進行修改,然後上傳新版本。我們以增加檔案內容為例。上傳時需勾選,覆蓋已存在的檔案。如下圖所示。 上傳成功後,我們檢視上傳歷史。會看到新上傳
SpringBoot進階之檔案上傳(單檔案上傳/多檔案上傳)
1.單檔案上傳 private String uploadPath="D:\\tomcat\\apache-tomcat-7.0.81-windows-x64\\apache-tomcat-7
一鍵jquery非同步上傳檔案(圖片)的實現(檔案上傳進度讀取未做)
本文采用boostrap、、jquery、jade(html的一種模板)、nodejs實現 最終實現的效果:一個按鈕在選擇完檔案後,自動上傳到後臺; -改變input樣式 在form表單中,用<a>包裹<input type="file">,讓i
Laravel框架學習(Request請求資料、Cookie、檔案上傳)
1、獲取Request請求資料 Laravel中一般通過控制器方法依賴注入來獲取當前請求的Request例項。 我們通過定義一個隱式控制器來進行本章節的測試。首先我們在routes.php定義路由如下: Route::controller('reque
29. Spring boot 檔案上傳(多檔案上傳)【從零開始學Spring Boot】
【視訊&交流平臺】 http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=40000000
Apache POI(檔案上傳下載)使用總結(超級詳細)
一、 POI簡介 Apache POI是Apache軟體基金會的開放原始碼函式庫,POI提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。二、 HSSF概況 HSSF 是Horrible SpreadS