Maison > php教程 > PHP源码 > le corps du texte

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

WBOY
Libérer: 2016-06-08 17:26:18
original
1499 Les gens l'ont consulté

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

<script>ec(2);</script>
 代码如下 复制代码

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 保存到文件路径


*/

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!