当前位置:Gxlcms > PHP教程 > 三款php多文件上传实例代码(1/3)_PHP教程

三款php多文件上传实例代码(1/3)_PHP教程

时间:2021-07-01 10:21:17 帮助过:54人阅读

三款php多文件上传实例代码在php开发应用中经常会碰到文件上传这个需,有时也会碰到要多文件上传,下面我们就来看看我提供的三款php多文件上传实例代码,好了费话不说多了来看看这些多文件上传功能适合你么。

三款php教程多文件上传实例代码
在php开发应用中经常会碰到文件上传这个需,有时也会碰到要多文件上传,下面我们就来看看我提供的三款php多文件上传实例代码,好了费话不说多了来看看这些多文件上传功能适合你么。

示例代码:


命名必是这样有"[]"

//设置允许用户上传的文件类型。
$type = array('gif', 'jpg', 'png', 'zip', 'rar');
$upload = new uploadfile($_files['uploadfile'], '/', 1024*1024, $type);
参数说明:1:表单的文件,2:上传目录,3:支持文件大小,4:允许文件类型
$icount = $upload->upload();
if($icount > 0) { //上传成功
print_r($upload->getsaveinfo());
*/

class uploadfile {
var $postfile = array(); // 用户上传的文件
var $custompath = ""; // 自定义文件上传路径
var $maxsize = ""; // 文件最大尺寸
var $lasterror = ""; // 最后一次出错信息
var $allowtype = array('gif', 'jpg', 'png', 'zip', 'rar', 'txt', 'doc', 'pdf');
var $endfilename = ""; // 最终保存的文件名
var $saveinfo = array(); // 保存文件的最终信息
var $root_dir = ""; // 项目在硬盘上的位置

/**
* 构造函数
* @access public
*/

function uploadfile($arrfile, $path="_", $size = 2097152, $type = 0) {
$this->postfile = $arrfile;
$this->custompath = $path == "_" ? "" : $path ;
$this->maxsize = $size;
if($type!=0) $this->allowtype = $arrfile;
$this->root_dir = $_server['document_root'];
$this->_mkdir($this->custompath);
}

/**
* 文件上传的核心代码
* @access public
* @return int 上传成功文件数
*/

function upload() {
$ilen = sizeof($this->postfile['name']);
for($i=0;$i<$ilen;$i++){
if ($this->postfile['error'][$i] == 0) { //上传时没有发生错误
//取当前文件名、临时文件名、大小、扩展名,后面将用到。
$sname = $this->postfile['name'][$i];
$stname = $this->postfile['tmp_name'][$i];
$isize = $this->postfile['size'][$i];
$stype = $this->postfile['type'][$i];
$sextn = $this->_getextname($sname);

//检测当前上传文件大小是否合法。
if($this->_checksize){
$this->lasterror = "您上传的文件[".$sname."],超过系统支持大小!";
$this->_showmsg($this->lasterror);
continue;
}

if(!is_uploaded_file($stname)) {
$this->lasterror = "您的文件不是通过正常途径上传!";
$this->_showmsg($this->lasterror);
continue;
}
$_filename = basename($sname,".".$sextn)."_".time().".".$sextn;
$this->endfilename = $this->custompath.$_filename;

if(!move_uploaded_file($stname, $this->root_dir.$this->endfilename)) {
$this->lasterror = $this->postfile['error'][$i];
$this->_showmsg($this->lasterror);
continue;
}

//存储当前文件的有关信息,以便其它程序调用。
$this->save_info[] = array("name" => $sname, "type" => $sextn, "size" => $isize, "path" => $this->endfilename);
}
}

return sizeof($this->save_info);
}

1 2 3

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444829.htmlTechArticle三款php多文件上传实例代码在php开发应用中经常会碰到文件上传这个需,有时也会碰到要多文件上传,下面我们就来看看我提供的三款php多...

人气教程排行