您好,登錄后才能下訂單哦!
小編給大家分享一下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" /> ( * 必須是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" > <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文件上傳類怎么用”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。