国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊(cè) PHP image upload class (added thumbnail)

PHP image upload class (added thumbnail)

Jul 06, 2016 pm 01:28 PM

There is a thumbnail function, but it feels incomplete and has some problems. Keep learning and modify it in the future

<form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post" ><input type="text" name="name" /><input type="file" name="file" /><input type="submit"  name='submit' value="提交" ></form>

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/28
 * Time: 21:04
 */

class upload{
      protected  $fileMine;//文件上傳類型
      protected  $filepath;//文件上傳路徑
      protected  $filemax;//文件上傳大小
      protected  $fileExt;//文件上傳格式
      protected  $filename;//文件名
      protected  $fileerror;//文件出錯(cuò)設(shè)置
      protected  $fileflag;//文件檢測(cè)
      protected  $fileinfo; //FILES
      protected  $ext;  //文件擴(kuò)展
      protected  $path;

    //文件上傳
    public function __construct($filename="file",$filemax=20000000,$filepath="./Uploads",$fileflag=true,$fileExt=array('jpg','exe'),$fileMine=array('image/jpeg'))
    {
        $this->filename=$filename;
        $this->fileinfo=$_FILES[$this->filename];
        $this->filemax=$filemax;
        $this->filepath=$filepath;
        $this->fileflag=$fileflag;
        $this->fileExt=$fileExt;
        $this->fileMine=$fileMine;

        //var_dump($this->filename);

    }

    //錯(cuò)誤判斷
    public function UpError(){

            if($this->fileinfo['error']>0){
                switch($this->fileinfo['error'])
                {
                    case 1:
                    $this->fileerror="上傳文件大小超過(guò)服務(wù)器允許上傳的最大值,php.ini中設(shè)置upload_max_filesize選項(xiàng)限制的值 ";
                        break;
                    case 2:
                        $this->fileerror="上傳文件大小超過(guò)HTML表單中隱藏域MAX_FILE_SIZE選項(xiàng)指定的值";
                        break;
                    case 3:
                        $this->fileerror="文件部分被上傳";
                        break;
                    case 4:
                        $this->fileerror="沒(méi)有選擇上傳文件";
                        break;
                    case 5:
                        $this->fileerror="未找到臨時(shí)目錄";
                        break;
                    case 6:
                        $this->fileerror="文件寫入失敗";
                        break;
                    case 7:
                        $this->fileerror="php文件上傳擴(kuò)展沒(méi)有打開(kāi) ";
                        break;
                    case 8:
                        $this->fileerror="";
                        break;

                }
                return false;
            }
            return true;

    }

    //檢測(cè)文件類型
    public function UpMine(){
        if(!in_array($this->fileinfo['type'],$this->fileMine)) {
            $this->error="文件上傳類型不對(duì)";
            return false;
        }
        return true;

    }
    //檢測(cè)文件格式
    public function UpExt(){
        $this->ext=pathinfo($this->fileinfo['name'],PATHINFO_EXTENSION);
        //var_dump($ext);
        if(!in_array($this->ext,$this->fileExt)){
            $this->fileerror="文件格式不對(duì)";
            return false;
        }
       return true;
    }
    //檢測(cè)文件路徑
    public function UpPath(){
        if(!file_exists($this->filepath)){
            mkdir($this->filepath,0777,true);
        }
    }
    //檢測(cè)文件大小
    public function UpSize(){
        $max=$this->fileinfo['size'];
        if($max>$this->filemax){
            $this->fileerror="文件過(guò)大";
            return false;
        }
        return true;
    }
    //檢測(cè)文件是否HTTP
    public function UpPost(){
        if(!is_uploaded_file($this->fileinfo['tmp_name'])){
            $this->fileerror="惡意上償還";
            return false;
        }
        return true;
    }
    //文件名防止重復(fù)
    public function Upname(){
        return md5(uniqid(microtime(true),true));
    }

    //圖片縮略圖
    public function Smallimg($x=100,$y=100){
        $imgAtt=getimagesize($this->path);
        //圖像寬,高,類型
        $imgWidth=$imgAtt[0];
        $imgHeight=$imgAtt[1];
        $imgext=$imgAtt[2];
        //等比列縮放

        if(($x/$imgWidth)>($y/$imgHeight)){
            $bl=$y/$imgHeight;
        }else{
            $bl=$x/$imgWidth;
        }
        $x=floor($imgWidth*$bl);  //縮放后
        $y=floor($imgHeight*$bl);
        $images=imagecreatetruecolor($x,$y);
        $big=imagecreatefromjpeg($this->path);
        imagecopyresized($images,$big,0,0,0,0,$x,$y,$imgWidth,$imgWidth);
        switch($imgext){
            case 1:
                $imageout=imagecreatefromgif($this->path);
                break;
            case 2:
                $imageout=imagecreatefromjpeg($this->path);
                break;
            case 3:
                $imageout=imagecreatefromgif($this->path);
                break;
        }
        $im=imagejpeg($images,$this->path);




    }

    //文件雙傳
    public function uploads()
    {
        if($this->UpError()&&$this->UpMine()&&$this->UpExt()&&$this->UpSize()&&$this->UpPost()){
            $this->UpPath();
            $names=$this->Upname();
            $this->path=$this->filepath.'/'. $names.'.'.$this->ext;

            if(move_uploaded_file($this->fileinfo['tmp_name'], $this->path)){
                return  $this->path;
            }else{
                $this->fileerror="上傳失敗";
            }
        }else{
            exit("<b>".$this->fileerror."</b>");
        }
    }





}




?>

 

<?php 
   header("content-type:imagejpeg");
header("Content-type:text/html;charset=utf-8");
  require 'list.php';
  $u=new upload();
  $a=$u->uploads();

  $c=$u->Smallimg();
echo "<img src={$a} />";
echo "<img src={$c} />";

?>

 

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<body>
    <form action="ce.php" enctype="multipart/form-data" method="post" >
    <input type="text" name="name" /><input type="file" name="file" />
    <input type="submit"  name='submit' value="提交" >
    </form>
</body>
</html>

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276