欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP實(shí)現(xiàn)的文件上傳類與用法詳解

 更新時(shí)間:2017年07月05日 10:23:40   作者:china_skag  
這篇文章主要介紹了PHP實(shí)現(xiàn)的文件上傳類與用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了PHP文件上傳類的定義與具體使用方法,需要的朋友可以參考下

本文實(shí)例講述了PHP實(shí)現(xiàn)的文件上傳類與用法。分享給大家供大家參考,具體如下:

FileUpload.class.php,其中用到了兩個(gè)常量,可在網(wǎng)站配置文件中定義:define('ROOT_PATH',dirname(__FILE__)); //網(wǎng)站根目錄、define('UPDIR','/uploads/'); //上傳主目錄

<?php
  //上傳文件類
  class FileUpload {
    private $error;  //錯(cuò)誤代碼
    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;  //臨時(shí)文件
    private $linkpath; //鏈接路徑
    private $linktotay; //今天目錄(相對(duì))
    //構(gòu)造方法,初始化
    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;
    }
    //移動(dòng)文件
    private function moveUpload() {
       if (is_uploaded_file($this->tmp)) {
         if (!move_uploaded_file($this->tmp,$this->setNewName())) {
            Tool::alertBack('警告:上傳失?。?);
         }
       } else {
         Tool::alertBack('警告:臨時(shí)文件不存在!');
       }
    }
    //設(shè)置新文件名
    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;
    }
    //驗(yàn)證目錄
    private function checkPath() {
       if (!is_dir($this->path) || !is_writeable($this->path)) {
         if (!mkdir($this->path)) {
            Tool::alertBack('警告:主目錄創(chuàng)建失??!');
         }
       }
       if (!is_dir($this->today) || !is_writeable($this->today)) {
         if (!mkdir($this->today)) {
            Tool::alertBack('警告:子目錄創(chuàng)建失??!');
         }
       }
    }
    //驗(yàn)證類型
    private function checkType() {
       if (!in_array($this->type,$this->typeArr)) {
         Tool::alertBack('警告:不合法的上傳類型!');
       }
    }
    //驗(yàn)證錯(cuò)誤
    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('警告:未知錯(cuò)誤!');
         }
       }
    }
  }
?>

其中,用到了一個(gè)靜態(tài)工具類 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();
     }     //彈窗賦值關(guān)閉
     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();
     } }
?>

下面進(jìn)行一個(gè)實(shí)例演示,請(qǐng)看下面的步驟:

1、先創(chuàng)建一個(gè) index.php 頁面,做一個(gè)表單

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a target=_blank  rel="external nofollow" rel="external nofollow" >http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>">
<html xmlns="<a target=_blank  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" style="display:none;" /> ( * 必須是jpg,gif,png,并且200k內(nèi)) <br />
     </form>
  </body>
</html>

2、創(chuàng)建 upfile.html 文件,建立表單提交到 upload.php.

upfile.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a target=_blank  rel="external nofollow" rel="external nofollow" >http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>">
<html xmlns="<a target=_blank  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" 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 文件調(diào)用文件上傳類實(shí)現(xiàn)上傳,并且把路徑賦給 input 標(biāo)簽和顯示圖片

<?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('警告:文件過大或者其他未知錯(cuò)誤導(dǎo)致瀏覽器崩潰!');
  }
?>

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php文件操作總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論