php 文件上传实例代码,本文章为你提供一款经典的php文件上传类了,并且举例验证了这一款文件上传代码是可用的哦。
- <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
- <html xmlns="http://www.111cn.net/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=gb2312" />
- <title>无标题文档</title>
- </head>
- <body>
- <form id="form1" name="form1" enctype="multipart/form-data" method="post" action="">
- <label for="filefield"></label>
- <input type="file" name="filefield" id="filefield" />
- <input type="submit" name="button" id="button" value="文件开始上传" />
- </form>
- </body>
- </html>
- <?
- /*
- | @param: $dir -- 存放目录,最后加"/" [字串]
- | @param: $file_var -- 表单变量 [字串]
- | @param: $max_size -- 设定最大上传值,以k为单位. [整数/浮点数]
- | @param: $type -- 限定后辍名(小写),多个用"/"隔开,不限定则留空 [字串]
- | @param: $name -- 上传后命名,留空则为原名,true为系统随机定名 [布林值]
- | return: 上传后文件名
- */
- function _asupfiles($dir, $file_var, $max_size='', $type='', $name=false)
- {
- if (!file_exists($dir)) showmsg("上传图片失败:上传目录 ".$dir." 不存在!",0);
- if (!is_writable($dir))
- {
- showmsg("上传图片失败:上传目录 ".$dir." 无法写入!",0);
- exit();
- }
- $upfile=& $_files["$file_var"];
- $upfilename = $upfile['name'];
- if (!($upfilename===''))
- {
- if (!is_uploaded_file($upfile['tmp_name']))
- {
- showmsg('上传图片失败:你选择的文件无法上传',0);
- exit();
- }
- if ($max_size>0 && $upfile['size']/1024>$max_size)
- {
- showmsg("上传图片失败:文件大小不能超过 ".$max_size."kb",0);
- exit();
- }
- $ext_name = strtolower(str_replace(".", "", strrchr($upfilename, ".")));
- if (!($type==='') && strpos($type, $ext_name)===false)
- {
- showmsg("上传图片失败:只允许上传 ".$type." 的文件!",0);
- exit();
- }
- ($name==true)?$uploadname=time().mt_rand(100,999).".".$ext_name :'';
- ($name==false)?$uploadname=$upfilename:'';
- !is_bool($name)?($uploadname=$name.".".$ext_name):'';
- //$uploadname = $name ? md5(uniqid(rand())).".".$ext_name : $upfilename;
- if (!move_uploaded_file($upfile['tmp_name'], $dir.$uploadname))
- {
- showmsg('上传图片失败:文件上传出错!',0);
- exit();
- }
- return $uploadname;
- }
- else
- {
- return '';
- }
- }
- ?>
新闻热点
疑难解答