search
Homephp教程php手册跟我学做PHP上传类

up.class.php

 

  class up{

    public $f; //表示$_FILES[xx]

    public $s; //上传文件的大小

public $n; //是否指定文件名

public $d; //是否指定文件夹

 

function __construct($f,$s=1,$n=false,$d=false){

 $this->f=$f;

 $this->s=$s*1000000;

 $this->n=$n;

 $this->d=$d;

 $this->loading();

}

  

  function loading(){

    $this->fsize();

$this->ftype();

$this->fmv();

  }

  

  function fname(){

    if($this->n){

return $this->n.$this->ftype();

}else{

return time().rand(111,999).$this->ftype();

}

  }

  

  function fdir(){

 if($this->d){  //判断用户是否指定文件夹名

if(!is_dir($this->d)){ //判断指定的文件夹名是否存在

mkdir($this->d);

}

return $this->d."/";

 }else{

 if(!is_dir(date("Ymd"))){

mkdir(date("Ymd"));

}

return date("Ymd")."/";

 }

  } //end fdir

  

  function fsize(){

    if($this->f['size']>$this->s){

echo "文件超过大小";

exit();

}else{

 return true;

}

  }

  

  function ftype(){

 switch($this->f['type']){

case "image/x-png": $ok=".png";

break;

case "image/png": $ok=".png";

break;

case "application/pdf": $ok=".pdf";

break;

case "image/pjpeg": $ok=".jpg";

break;

case "image/jpeg": $ok=".jpg";

break;

case "image/jpg": $ok=".jpg";

break;

default: $ok=false;

break;

}

if($ok){

  return $ok;

}else{

 echo "文件类型不正确";

 exit();

}

  }

  

   function fmv(){

     move_uploaded_file($this->f['tmp_name'],$this->fdir().$this->fname());

   }

  

  }

 

 

?>

 

test.php 上传文件

 

include("up.class.php");

 if(!empty($_POST['sub'])){

  foreach($_FILES as $id=>$v){

  new up($_FILES[$id]);

  }

 }

 ?>







 

 



Statement
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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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 Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.