您好,登錄后才能下訂單哦!
使用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" "<a target=_blank href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" rel="external nofollow" rel="external nofollow" >http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>"> <html xmlns="<a target=_blank href="http://www.w3.org/1999/xhtml" rel="external nofollow" rel="external nofollow" >http://www.w3.org/1999/xhtml</a>"> <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" "<a target=_blank href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" rel="external nofollow" rel="external nofollow" >http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>"> <html xmlns="<a target=_blank href="http://www.w3.org/1999/xhtml" rel="external nofollow" rel="external nofollow" >http://www.w3.org/1999/xhtml</a>"> <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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。