首页 > 编程语言 >PHP设计超级好用的文件上传处理类一 (37)

PHP设计超级好用的文件上传处理类一 (37)

时间:2022-11-30 12:03:06浏览次数:32  
标签:function return name 37 private 类一 false PHP 上传

<?php
class FileUpload {
private $filepath; //指定上传文件保存的路径
private $allowtype=array('gif', 'jpg', 'png', 'jpeg'); //充许上传文件的类型
private $maxsize=1000000; //允上传文件的最大长度 1M
private $israndname=true; //是否随机重命名, true false不随机,使用原文件名
private $originName; //源文件名称
private $tmpFileName; //临时文件名
private $fileType; //文件类型
private $fileSize; //文件大小
private $newFileName; //新文件名
private $errorNum=0; //错误号
private $errorMess=""; //用来提供错误报告



//用于对上传文件初使化
//1. 指定上传路径, 2,充许的类型, 3,限制大小, 4,是否使用随机文件名称
//让用户可以不用按位置传参数,后面参数给值不用将前几个参数也提供值
function __construct($options=array()){
foreach($options as $key=>$val){
$key=strtolower($key);
//查看用户参数中数组的下标是否和成员属性名相同
if(!in_array($key,get_class_vars(get_class($this)))){
continue;
}

$this->setOption($key, $val);
}


}



private function getError(){
$str="上传文件<font color='red'>{$this->originName}</font>时出错:";

switch($this->errorNum){
case 4: $str .= "没有文件被上传"; break;
case 3: $str .= "文件只被部分上传"; break;
case 2: $str .= "上传文件超过了HTML表单中MAX_FILE_SIZE选项指定的值"; break;
case 1: $str .= "上传文件超过了php.ini 中upload_max_filesize选项的值"; break;
case -1: $str .= "末充许的类型"; break;
case -2: $str .= "文件过大,上传文件不能超过{$this->maxSize}个字节"; break;
case -3: $str .= "上传失败"; break;
case -4: $str .= "建立存放上传文件目录失败,请重新指定上传目录"; break;
case -5: $str .= "必须指定上传文件的路径"; break;

default: $str .= "末知错误";
}

return $str.'<br>';
}

//用来检查文件上传路径
private function checkFilePath(){
if(empty($this->filepath)) {
$this->setOption('errorNum', -5);
return false;
}

if(!file_exists($this->filepath) || !is_writable($this->filepath)){
if(!@mkdir($this->filepath, 0755)){
$this->setOption('errorNum', -4);
return false;
}
}
return true;
}
//用来检查文件上传的大小
private function checkFileSize() {
if($this->fileSize > $this->maxsize){
$this->setOPtion('errorNum', '-2');
return false;
}else{
return true;
}
}

//用于检查文件上传类型
private function checkFileType() {
if(in_array(strtolower($this->fileType), $this->allowtype)) {
return true;
}else{
$this->setOption('errorNum', -1);
return false;
}
}
//设置上传后的文件名称
private function setNewFileName(){
if($this->israndname){
$this->setOption('newFileName', $this->proRandName());
} else {
$this->setOption('newFileName', $this->originName);
}
}



//设置随机文件名称
private function proRandName(){
$fileName=date("YmdHis").rand(100,999);
return $fileName.'.'.$this->fileType;
}

private function setOption($key, $val){
$this->$key=$val;
}
//用来上传一个文件
function uploadFile($fileField){
$return=true;
//检查文件上传路径
if(!$this->checkFilePath()){
$this->errorMess=$this->getError();
return false;
}


$name=$_FILES[$fileField]['name'];
$tmp_name=$_FILES[$fileField]['tmp_name'];
$size=$_FILES[$fileField]['size'];
$error=$_FILES[$fileField]['error'];

if(is_Array($name)){
$errors=array();

for($i=0; $i<count($name); $i++){
if($this->setFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i])){
if(!$this->checkFileSize() || !$this->checkFileType()){
$errors[]=$this->getError();
$return=false;
}
}else{
$error[]=$this->getError();
$return=false;
}

if(!$return)
$this->setFiles();
}

if($return){
$fileNames=array();

for($i=0; $i<count($name); $i++){
if($this->setFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i])){
$this->setNewFileName();
if(!$this->copyFile()){
$errors=$this->getError();
$return=false;
}else{
$fileNames[]=$this->newFileName;
}
}
}

$this->newFileName=$fileNames;
}

$this->errorMess=$errors;
return $return;
} else {

if($this->setFiles($name, $tmp_name, $size, $error)){
if($this->checkFileSize() && $this->checkFileType()){
$this->setNewFileName();

if($this->copyFile()){
return true;
}else{
$return=false;
}

}else{
$return=false;
}
}else{
$return=false;
}



if(!$return)
$this->errorMess=$this->getError();


return $return;
}
}

private function copyFile(){
if(!$this->errorNum){
$filepath=rtrim($this->filepath, '/').'/';
$filepath.=$this->newFileName;

if(@move_uploaded_file($this->tmpFileName, $filepath)) {
return true;
}else{
$this->setOption('errorNum', -3);
return false;
}

}else{
return false;
}
}

//设置和$_FILES有关的内容
private function setFiles($name="", $tmp_name='', $size=0, $error=0){

$this->setOption('errorNum', $error);

if($error){
return false;
}

$this->setOption('originName', $name);
$this->setOption('tmpFileName', $tmp_name);
$arrStr=explode('.', $name);
$this->setOption('fileType', strtolower($arrStr[count($arrStr)-1]));
$this->setOption('fileSize', $size);

return true;
}

//用于获取上传后文件的文件名
function getNewFileName(){
return $this->newFileName;
}
//上传如果失败,则调用这个方法,就可以查看错误报告
function getErrorMsg() {
return $this->errorMess;
}
}

 

源码面前,了无秘密



标签:function,return,name,37,private,类一,false,PHP,上传
From: https://blog.51cto.com/zhenghongxin/5898212

相关文章

  • PHP设计日历类一 (38)
    由两个文件组成:第一个test.php<style>table{border:1pxsolid#050;}.fontb{color:white;background:blue;}th{......
  • Thinkphp入门 四 —布局、缓存、系统变量 (48)
    【控制器操作方法参数设置】​​http://网址/index.php/控制器/操作方法​​  【页面跳转】【变量调节器】Smarty变量调节器TP变量调节器:普通的php函数(count strlen ......
  • 十二生肖查询网页版制作(php)
    今天无聊做了一个十二生肖查询器:预览网址效果:​​http://hongxing01.hktd02u.me48.com/03Sxcx​​源代码下载:​​http://down.51cto.com/data/1985014​​这个Demo的学习很......
  • 【开发小技巧】02-如何使用canvasJS在PHP中制作动态图表?
    来源| https://www.geeksforgeeks.org/how-to-make-dynamic-chart-in-php-using-canvasjs/CanvasJS是一个JavaScript库,用于轻松为网页创建其他类型的图表。例如条形图,饼图......
  • PHP获取今天,昨天,本月,上个月,本年 起始时间戳
    https://cloud.tencent.com/developer/article/1885951?from=15425date_default_timezone_set("Asia/Shanghai");//设置为上海时间否则开始时间会相差8个小时//获取......
  • 题解 CF1370B
    题解CF1370B这个题跟脑筋急转弯一样诶\(gcd\)这个东西他有很多种可能性,但是如果我们考虑最简单的数字性质奇偶,就会发现,其实所有偶数的\(gcd\)都是\(2\)对吧所以,我......
  • PHP期末复习简答题
    请简述Apache和PHP的工作原理用户再浏览器输入要访问的地址Apache服务器解析用户的请求,其后缀是“.php”,则将用户的请求交给PHP处理;若是静态的HTML文件、CSS文件和JavaS......
  • 379. 捉迷藏
    题目链接379.捉迷藏Vani和cl2在一片树林里捉迷藏。这片树林里有\(N\)座房子,\(M\)条有向道路,组成了一张有向无环图。树林里的树非常茂密,足以遮挡视线,但是沿着道......
  • 376. 机器任务
    题目链接376.机器任务有两台机器\(A,B\)以及\(K\)个任务。机器\(A\)有\(N\)种不同的模式(模式\(0\simN-1\)),机器\(B\)有\(M\)种不同的模式(模式\(0\simM......
  • 从零开始学Python【37】--朴素贝叶斯模型(理论部分)
    【知识铺垫】在介绍如何使用贝叶斯概率公式计算后验概率之前,先回顾一下概率论与数理统计中的条件概率和全概率公式:如上等式为条件概率的计算公式,表示在已知事件A的情况下事......