Home > php教程 > PHP源码 > body text

文件上传php代码(使用简单)

WBOY
Release: 2016-06-08 17:26:18
Original
1413 people have browsed it

这是一款使用方便简单的php文件上传函数,只你要设置三个参数就可以方便的把你指定的文件类型上传批指定的目录了。

 代码如下 复制代码

function upfile($filename,$type,$path)
{

 $tempfile=$_files[$filename];//接收上传的临时文件

 //有没有上传成功
 if($error=$tempfile["error"])
 {
  switch($error){
   case 1:$errorstr="上传的文件超过了2m请返回检查。";break;
   case 2:$errorstr="上传文件的大小超过了 html 表单中限定值8m";break;
   case 3:$errorstr="文件只有部分被上传";break;
   case 4:$errorstr="没有文件被上传";break;
   case 6:$errorstr="找不到临时文件夹";break;
   case 7:$errorstr="文件写入失败";break;
  }
  showmsg($errorstr);
 }

 //文件类型是否在允许的范围内
 $filename=$tempfile["name"];//取上传原文件名
 $tmparr=explode(".",$filename);
 $extname=strtolower($tmparr[count($tmparr)-1]);//取出原扩展名并转为小写字母
 if(!in_array($extname,$type)) showmsg("上传的文件类型不允许,请返回检查.");

 //改文件名
 $newfile="zy".time().rand(100,1000).".".$extname;//构成新的文件名

 //路径
 $path.=$newfile;

 //重名判断
 if(file_exists($path)){
  $newfile="zy".time().rand(100,1000).".".$extname;//构成新的文件名
  $path.=$newfile;
 }

 if(!move_uploaded_file($tempfile['tmp_name'],$path))showmsg("文件移动失败。");

 return $newfile;
}

//些上传函数调用方法

 代码如下 复制代码
upfile($filename,$type,$path);
/*
参数:
$filename  为上传表单 type=file的名称
$type 允许上传的文件类型
$path 保存到文件路径


*/

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!