search
Homephp教程php手册支持多文件上传php文件上传代码

我们下面是个文件图片上传类,调用方法 upfile($path = "./", $format = "", $maxsize = 0, $over = 0) :构造函数 参数(保存路径,上传格式,上传最大字节数,是否覆盖同名文件),如果你要上传多文件就foreach($_FILES as $key=>$val)再 $filear[] = $f->upload($key);//返回上传后文件名数组。

 */

 代码如下 复制代码

class upfile{
 //上传文件信息
 var $filename;
 // 保存名
 var $savename;
 // 保存路径
 var $savepath;
 // 文件格式限定,为空时不限制格式
 var $format = "";
 // 覆盖模式
 var $overwrite = 0;
 /* $overwrite = 0 时不覆盖同名文件
  * $overwrite = 1 时覆盖同名文件
  */
 //文件最大字节
 var $maxsize = 210000000;
 //文件扩展名
 var $ext;
 
 /* 构造函数
  * $path 保存路径
  * $format 文件格式(用逗号分开)
  * $maxsize 文件最大限制,0为默认值
  * $over 复盖参数
  */
 function upfile($path = "./", $format = "", $maxsize = 0, $over = 0){
  if(!file_exists($path)){
   $this->halt("指定的目录[ ".$path." ]不存在。");
  }
 
  if(!is_writable($path)){
   $this->halt("指定的目录[ ".$path." ]不可写。");
  }
  $path = str_replace("","/", $path);
  $this->savepath = substr($path, -1) == "/" ? $path : $path."/";//保存路径
 
  $this->overwrite = $over;//是否复盖相同名字文件
  $this->maxsize = !$maxsize ? $this->maxsize : $maxsize;//文件最大字节
  $this->format = $format;
 }
 
 /*
  * 功能:检测并组织文件
  * $form      文件域名称
  * $filename 上传文件保存名称,为空或者上传多个文件时由系统自动生成名称
  * $filename = 1,并上传多个同文件域名称文件时,则文件保存为原上传文件名称。
  */
 function upload($form, $filename = ""){
  if(!isset($_files[$form])){
   $this->halt("指定的文件域名称不存在。");
  }else{
   $filear = $_files[$form];
  }
 
  if(is_array($filear["name"])){//上传同文件域名称多个文件
   $outfile = array();//返回文件名称数组
   for($i = 0; $i     $ar["name"] = $filear["name"][$i];
    $ar["tmp_name"] = $filear["tmp_name"][$i];
    $ar["size"] = $filear["size"][$i];
    $ar["error"] = $filear["error"][$i];
   
    $this->getext($ar["name"]);//取得扩展名
    $this->set_savename($filename == 1 ? $ar["name"] : "");//设置保存文件名
    $outfile[] = $this->copyfile($ar);
   }
   return $outfile;
  }else{//上传单个文件
   $this->getext($filear["name"]);//取得扩展名
   $this->set_savename($filename);//设置保存文件名
   return $this->copyfile($filear);
  }
  return false;
 }
 
 /*
  * 功能:检测并复制上传文件
  * $filear 上传文件资料数组
  */
 function copyfile($filear){
 
  if($filear["size"] > $this->maxsize){
   $this->halt("上传文件 ".$filear["name"]." 大小超出系统限定值[".$this->maxsize." 字节],不能上传。");
  }
 
  if(!$this->overwrite && file_exists($this->savename)){
   $this->halt($this->savename." 文件名已经存在。");
  }
 
  if(!$this->chkext()){
   $this->halt($this->ext." 文件格式不允许上传。");
  }
 
  if(!copy($filear["tmp_name"], $this->savepath.$this->savename)){
   $errors = array(0=>"文件上传成功",
       1=>"上传的文件超过了 php教程.ini 中 upload_max_filesize 选项限制的值。 ",
       2=>"上传文件的大小超过了 html 表单中 max_file_size 选项指定的值。 ",
       3=>"文件只有部分被上传。 ",
       4=>"没有文件被上传。 ");
   $this->halt($errors[$filear["error"]]);
  }else{
   @unlink($filear["tmp_name"]);//删除临时文件
   return $this->savename;//返回上传文件名
  }
 }
 
 /*
  * 功能: 取得文件扩展名
  * $filename 为文件名称
  */
 function getext($filename){
  if($filename == "") return;
 
  $ext = explode(".", $filename);
 
  $this->ext = $ext[count($ext)-1];
 }
 
 /*
  * 功能:检测文件类型是否允许
  */
 function chkext(){
  if($this->format == "" || in_array(strtolower($this->ext), explode(",", strtolower($this->format)))) return true;
  else return false;
 }
 /*
  * 功能: 设置文件保存名
  * $savename 保存名,如果为空,则系统自动生成一个随机的文件名
  */
 function set_savename($savename = ""){
  if ($savename == "") { // 如果未设置文件名,则生成一个随机文件名
   srand ((double) microtime() * 1000000);
   $rnd = rand(100,999);
   $name = date('u') + $rnd;
   $name = $name.".".$this->ext;
  } else {
   $name = $savename;
  }
  $this->savename = $name;
 }
 
 /*
  * 功能:错误提示
  * $msg 为输出信息
  */
 function halt($msg){
  echo "注意:".$msg;
  exit;
 }
 
/*
*
*主要用来删除已上传的文件,不返回
*参数$file :文件路径
*/

function delete_file($file)
{
    if (file_exists($file))
    {
        $delete = chmod ($file, 0777);
        $delete = unlink($file);
        if(file_exists($file))
        {
            $filesys = eregi_replace("/","",$file);
            $delete = system("del $filesys");
            clearstatcache();
            if(file_exists($file))
            {
                $delete = chmod ($file, 0777);
                $delete = unlink($file);
                $delete = system("del $filesys");
            }
        }
        clearstatcache();
    }
}
 
 
}

/*
使用方法

* 文件上传类
 upfile($path = "./", $format = "", $maxsize = 0, $over = 0)  :构造函数 参数(保存路径,上传格式,上传最大字节数,是否覆盖同名文件)
 
 
 
 * 实例:
 


 
//上传单文件
  if(isset($_files["files"]))
  {
     $filear=array();
     $filear = $f->upload("files");//返回上传后文件名
  echo $filear;
 
  }
 


 
 

//上传多个不同文件域名称文件

 代码如下 复制代码
if(isset($_files){
 foreach($_files as $key=>$val)
  $filear[] = $f->upload($key);//返回上传后文件名数组
}


*/



教程网址:

欢迎收藏∩_∩但请保留本文链接。

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 Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

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.