찾다
php教程php手册PHP之图片上传类(加了缩略图)

有缩略图功能 但是 感觉不全面,而且有点问题,继续学习,将来以后修改下

<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;//文件出错设置
      protected  $fileflag;//文件检测
      protected  $fileinfo; //FILES
      protected  $ext;  //文件扩展
      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);

    }

    //错误判断
    public function UpError(){

            if($this->fileinfo['error']>0){
                switch($this->fileinfo['error'])
                {
                    case 1:
                    $this->fileerror="上传文件大小超过服务器允许上传的最大值,php.ini中设置upload_max_filesize选项限制的值 ";
                        break;
                    case 2:
                        $this->fileerror="上传文件大小超过HTML表单中隐藏域MAX_FILE_SIZE选项指定的值";
                        break;
                    case 3:
                        $this->fileerror="文件部分被上传";
                        break;
                    case 4:
                        $this->fileerror="没有选择上传文件";
                        break;
                    case 5:
                        $this->fileerror="未找到临时目录";
                        break;
                    case 6:
                        $this->fileerror="文件写入失败";
                        break;
                    case 7:
                        $this->fileerror="php文件上传扩展没有打开 ";
                        break;
                    case 8:
                        $this->fileerror="";
                        break;

                }
                return false;
            }
            return true;

    }

    //检测文件类型
    public function UpMine(){
        if(!in_array($this->fileinfo['type'],$this->fileMine)) {
            $this->error="文件上传类型不对";
            return false;
        }
        return true;

    }
    //检测文件格式
    public function UpExt(){
        $this->ext=pathinfo($this->fileinfo['name'],PATHINFO_EXTENSION);
        //var_dump($ext);
        if(!in_array($this->ext,$this->fileExt)){
            $this->fileerror="文件格式不对";
            return false;
        }
       return true;
    }
    //检测文件路径
    public function UpPath(){
        if(!file_exists($this->filepath)){
            mkdir($this->filepath,0777,true);
        }
    }
    //检测文件大小
    public function UpSize(){
        $max=$this->fileinfo['size'];
        if($max>$this->filemax){
            $this->fileerror="文件过大";
            return false;
        }
        return true;
    }
    //检测文件是否HTTP
    public function UpPost(){
        if(!is_uploaded_file($this->fileinfo['tmp_name'])){
            $this->fileerror="恶意上偿还";
            return false;
        }
        return true;
    }
    //文件名防止重复
    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} / alt="PHP之图片上传类(加了缩略图)" >";
echo "<img  src={$c} / alt="PHP之图片上传类(加了缩略图)" >";

?>

  

<!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>

  

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

에디트플러스 중국어 크랙 버전

에디트플러스 중국어 크랙 버전

작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

맨티스BT

맨티스BT

Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

mPDF

mPDF

mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

MinGW - Windows용 미니멀리스트 GNU

MinGW - Windows용 미니멀리스트 GNU

이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.