本文實例講述了PHP文件上傳類。分享給大家供大家參考,具體如下:
這裡演示了FileUpload.class.php文件上傳類,其中用到了兩個常量,可在網站配置文件中定義:
define('ROOT_PATH',dirname(__FILE__)); //網站根目錄 define('UPDIR','/uploads/'); //上傳主目錄
具體代碼如下:
<?php //上傳文件類 class FileUpload { private $error; //錯誤代碼 private $maxsize; //表單最大值 private $type; //類型 private $typeArr = array('image/jpeg','image/pjpeg','image/png','image/x-png','image/gif'); //類型合集 private $path; //目錄路徑 private $today; //今天目錄 private $name; //文件名 private $tmp; //臨時文件 private $linkpath; //鏈接路徑 private $linktotay; //今天目錄(相對) //構造方法,初始化 public function __construct($_file,$_maxsize) { $this->error = $_FILES[$_file]['error']; $this->maxsize = $_maxsize / 1024; $this->type = $_FILES[$_file]['type']; $this->path = ROOT_PATH.UPDIR; $this->linktotay = date('Ymd').'/'; $this->today = $this->path.$this->linktotay; $this->name = $_FILES[$_file]['name']; $this->tmp = $_FILES[$_file]['tmp_name']; $this->checkError(); $this->checkType(); $this->checkPath(); $this->moveUpload(); } //返回路徑 public function getPath() { $_path = $_SERVER["SCRIPT_NAME"]; $_dir = dirname(dirname($_path)); if ($_dir == '\\') $_dir = '/'; $this->linkpath = $_dir.$this->linkpath; return $this->linkpath; } //移動文件 private function moveUpload() { if (is_uploaded_file($this->tmp)) { if (!move_uploaded_file($this->tmp,$this->setNewName())) { Tool::alertBack('警告:上傳失敗!'); } } else { Tool::alertBack('警告:臨時文件不存在!'); } } //設置新文件名 private function setNewName() { $_nameArr = explode('.',$this->name); $_postfix = $_nameArr[count($_nameArr)-1]; $_newname = date('YmdHis').mt_rand(100,1000).'.'.$_postfix; $this->linkpath = UPDIR.$this->linktotay.$_newname; return $this->today.$_newname; } //驗證目錄 private function checkPath() { if (!is_dir($this->path) || !is_writeable($this->path)) { if (!mkdir($this->path)) { Tool::alertBack('警告:主目錄創建失敗!'); } } if (!is_dir($this->today) || !is_writeable($this->today)) { if (!mkdir($this->today)) { Tool::alertBack('警告:子目錄創建失敗!'); } } } //驗證類型 private function checkType() { if (!in_array($this->type,$this->typeArr)) { Tool::alertBack('警告:不合法的上傳類型!'); } } //驗證錯誤 private function checkError() { if (!empty($this->error)) { switch ($this->error) { case 1 : Tool::alertBack('警告:上傳值超過了約定最大值!'); break; case 2 : Tool::alertBack('警告:上傳值超過了'.$this->maxsize.'KB!'); break; case 3 : Tool::alertBack('警告:只有部分文件被上傳!'); break; case 4 : Tool::alertBack('警告:沒有任何文件被上傳!'); break; default: Tool::alertBack('警告:未知錯誤!'); } } } } ?>
其中,用到了一個靜態工具類 Tool.class.php,代碼如下:
Tool.class.php
<?php class Tool { //彈窗返回 static public function alertBack($_info) { echo "<script type='text/javascript'>alert('$_info');history.back();</script>"; exit(); } //彈窗賦值關閉 static public function alertOpenerClose($_info,$_path) { echo "<script type='text/javascript'>alert('$_info');</script>"; echo "<script type='text/javascript'>opener.document.content.thumbnail.value='$_path';</script>"; echo "<script type='text/javascript'>opener.document.content.pic.style.display='block';</script>"; echo "<script type='text/javascript'>opener.document.content.pic.src='$_path';</script>"; echo "<script type='text/javascript'>window.close();</script>"; exit(); } } ?>
下面進行一個實例演示,請看下面的步驟:
1、先創建一個 index.php 頁面,做一個表單
index.php
<!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>main</title> </head> <body> <form name="content" method="post" action="?action=add"> <input type="text" name="thumbnail" class="text" readonly="readonly" /> <input type="button" value="上傳" onclick="centerWindow('./upfile.html','upfile','400','100')" /> <img name="pic" style="display:none;" /> ( * 必須是jpg,gif,png,並且200k內) <br /> </form> </body> </html>
2、創建 upfile.html 文件,建立表單提交到 upload.php
upfile.html
<!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>上傳圖片</title> </head> <body></p><p> <form method="post" action="./upload.php" enctype="multipart/form-data" style="text-align:center;margin:30px;"> <input type="hidden" name="MAX_FILE_SIZE" value="204800" /> <input type="file" name="pic" /> <input type="submit" name="send" value="確定上傳" /> </form></p><p></body> </html>
3、通過 upload.php 文件調用文件上傳類實現上傳,並且把路徑賦給 input 標簽和顯示圖片
<?php require 'FileUpload.class.php'; if (isset($_POST['send'])) { $_fileupload = new FileUpload('pic',$_POST['MAX_FILE_SIZE']); $_path = $_fileupload->getPath(); Tool::alertOpenerClose('文件上傳成功!',$_path); } else { Tool::alertBack('警告:文件過大或者其他未知錯誤導致浏覽器崩潰!'); } ?>
更多關於PHP相關內容感興趣的讀者可查看本站專題:《php文件操作總結》、《PHP運算與運算符用法總結》、《PHP網絡編程技巧總結》、《PHP基本語法入門教程》、《php操作office文檔技巧總結(包括word,excel,access,ppt)》、《php日期與時間用法總結》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。