search
Homephp教程PHP源码文件上传php代码(使用简单)

这是一款使用方便简单的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 保存到文件路径


*/

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development 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),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.