当前位置:网站首页 > 软件教程 > 正文

PHP上传文件类的实现代码与相关使用技巧

作者:admin发布时间:2021-11-02分类:软件教程浏览:评论:0


导读:本文实例讲述了PHP文件上传类。分享给大家供大家参考,具体如下:这里演示了FileUpload.class.php文件上传类,其中用到了两个常量,可在网站配置文件中定义:12define('ROOT_PATH',dirname(__FILE__));//网站根目录define('UPDIR','/uploads/')

文中案例叙述了PHP文件提交类。共享给大伙儿供各位参照,实际方式:

这儿演试了FileUpload.class.php文件提交类,在其中使用了2个变量定义,可在网址环境变量中界定:

1 2 define('ROOT_PATH',dirname(._FILE._)); //网址网站根目录 define('UPDIR','/uploads/'); //提交主目录

实际编码如下所示:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 <?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('警示:未知错误!');          }        }     }   } ?>

在其中,使用了一个静态数据java工具 Tool.class.php,编码如下所示:

Tool.class.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?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

1 2 3 4 5 6 7 8 9 10 11 12 13 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/zb_users/upload/2021/11/02/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

1 2 3 4 5 6 7 8 9 10 11 12 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/zb_users/upload/2021/11/02/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 标识和表明照片

1 2 3 4 5 6 7 8 9 10 <?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


欢迎 发表评论: