700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 最新的php 文件上传模型 支持多文件上传【PHP】

最新的php 文件上传模型 支持多文件上传【PHP】

时间:2020-04-11 05:49:15

相关推荐

最新的php 文件上传模型 支持多文件上传【PHP】

后端开发|php教程

php,多文件上传

后端开发-php教程

网站源码 app,ubuntu怎么ping通,爬虫的安全机制,PHP完整模板,seo品牌培训lzw

<?php

class UploadModel

{

protected $keys;

protected $err = array();

protected $target;

protected $exts;

protected $maxSize;

protected $randName;

protected $files = array();

/**

* 初始化变量

*/

public function __construct()

{

$this->exts = array(jpeg,jpg,gif,png,zip, ar);

$this->maxSize = 1024*1024*2;

$this->target = dirname(__FILE__) . /upload/;

$this->randName = true;

$this->keys = $this->getKeys();

}

/**

* 获取 file 的名称

*/

protected function getKeys()

{

$keys = array_keys($_FILES);

return $keys;

}

/**

* 设置不同类型的变量

*/

public function __set($name, $value)

{

$property = array( arget,exts,maxSize, andName);

if(!in_array($name, $property)) return false;

switch(strval($name))

{

case arget:

$this->$name = Configure::read(app_path) . $value;

break;

case exts:

$this->$name = explode(,, $value);

break;

case andName:

if($value === true || $value == 1)

{

$this->$name = true;

}

else {

$this->$name = false;

}

break;

default:

$this->$name = $value;

}

}

/**

* 移动上传的文件到指定的目录

* @param $fileName 移动单个文件名称的时候,对上传的文件重新命名

*/

public function save($fileName)

{

$this->err = array();

$this->files = array();

if(!file_exists($this->target)) {

mkdir($this->target);

chmod($this->target, 0777);

}

foreach($this->keys as $key)

{

if(is_array($_FILES[$key][ ame]))

{

$count = count($_FILES[$key][ ame]);

for($i=0; $i<$count; $i++)

{

$keys = array_keys($_FILES[$key]);

foreach($keys as $item)

{

$file[$item] = $_FILES[$key][$item][$i];

}

$this->upload($file, $fileName);

}

return (count($this->err) > 0)? false:true;

}

else {

return $this->upload($_FILE[$key], $fileName);

}

}

}

/** 内部处理上传文件的过程 */

protected function upload($file, $fileName)

{

if(!is_uploaded_file($file[ mp_name])) return false;

if(!$this->checkExt($file)) return false;

if(!$this->checkSize($file)) return false;

if($this->randName)

{

$newFileName = $this->target . date(YmdHis, time()) . rand(0,9) . . . $this->getExt($file[ ame]);

}

elseif(emptyempty($fileName))

{

$newFileName = $this->target . / . $file[ ame];

}

else {

$newFileName = $this->target . / . $fileName;

}

$result = move_uploaded_file($file[ mp_name], $newFileName);

if(!$result)

{

return false;

}

else {

$this->files[] = str_replace($this->target, $newFileName);

return true;

}

}

/**

* 是否是可上传的文件类型

* @param $file 文件对象

*/

public function checkExt($file)

{

if(!in_array($this->getExt($file[ ame]), $this->exts))

{

$this->err[] = $file[ ame].:ext;

return false;

}

else {

return true;

}

}

/**

* 获取文件后缀名

*/

public function getExt($fileName)

{

$pos = strrpos($fileName, .)+1;

return substr($fileName, $pos);

}

/**

* 检查文件大小是否合法

*/

public function checkSize($file)

{

if($size > $this->maxSize)

{

$this->err[] = $file[ ame].:max;

return false;

}

else {

return true;

}

}

/**

* 取得已经上传的文件名称

*/

public function getFiles()

{

return $this->files;

}

}

使用实例:

include uploaded.model.php;

$U = new UploadModel();

$U->target = /tmp/;

$U->exts = jpg,gif;

$U->maxSize = 1024*275; //275KB

$U->save();

$files = $U->getFiles();

print_r($files);

include uploaded.model.php;

$U = new UploadModel();

$U->target = /tmp/;

$U->exts = jpg,gif;

$U->maxSize = 1024*275; //275KB

$U->save();

$files = $U->getFiles();

print_r($files);

在 MayFish 里的使用实例:

动易cms后台源码,树莓派ubuntu安装,h5怎么发送到tomcat,保育类爬虫动物,php文件怎么改mp4,seo內链lzw

public function up()

{

$U = M(SYS, upload);

$U->target = /tmp/;

$U->exts = jpg,gif;

$U->maxSize = 1024*275; //275KB

$U->save();

header(Location:/?a=upload);

}

public function up()

{

$U = M(SYS, upload);

$U->target = /tmp/;

$U->exts = jpg,gif;

$U->maxSize = 1024*275; //275KB

$U->save();

header(Location:/?a=upload);

}

前台代码:

网赚广告源码,ubuntu关闭蓝牙命令,tomcat7和8能共存吗,有关爬虫的代码,php设计模式之原型模式详解,samuel seo韩国lzw

无标题文档

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。