首页 > php教程 > php手册 > 正文

跟我学做PHP上传类

php中文网
发布: 2016-06-21 08:50:57
原创
1169人浏览过

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]);

  }

 }

 ?>







 

 



PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号