txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid,jar,jad,exe,html,htm,css,js,doc上传,音乐文件等都可以,实例代码如下:
- <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <title>php文件上传</title>
- </head>
- <body>
- <form action="uploadsir.php" method="post" enctype="multipart/form-data"><input name="filedata" type="file" id="filedata" />
- <input type="submit" name="submit" value="上传" /></form>
- <?php
- if(!$_post)die();
- $state=uploadfile('filedata');
- if($state['err']){
- die('<script>alert("上传出错:'.$state['msg'].'");history.go(-1);</script>');
- }
- echo'<object type="application/x-shockwave-flash" data="template/images/copy.swf?u='.weburl.$state['msg'].'" width="100" height="40">
- <param name="movie" value="copy.swf?u='.weburl.$state['msg'].'" />';
- function uploadfile($inputname)
- {
- $immediate=$_get['immediate'];
- $attachdir='../pictures';//上传文件保存路径,结尾不要带/
- $urldir="../pictures";
- $dirtype=2;//1:按天存入目录 2:按月存入目录 3:按扩展名存目录 建议使用按天存
- $maxattachsize=2097152;//最大上传大小,默认是2m
- $upext='txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid,jar,jad,exe,html,htm,css,js,doc';//上传扩展名
- $err = "";
- $msg = "";
- $upfile=$_files[$inputname];
- if(!emptyempty($upfile['error']))
- {
- switch($upfile['error'])
- {
- case '1':
- $err = '文件大小超过了php.ini定义的upload_max_filesize值';
- break;
- case '2':
- $err = '文件大小超过了html定义的max_file_size值';
- break;
- case '3':
- $err = '文件上传不完全';
- break;
- case '4':
- $err = '无文件上传';
- break;
- case '6':
- $err = '缺少临时文件夹';
- break;
- case '7':
- $err = '写文件失败';
- break;
- case '8':
- $err = '上传被其它扩展中断';
- break;
- case '999':
- default:
- $err = '无有效错误代码';
- }
- }
- elseif(emptyempty($upfile['tmp_name']) || $upfile['tmp_name'] == 'none')$err = '无文件上传';
- else
- {
- $temppath=$upfile['tmp_name'];
- $fileinfo=pathinfo($upfile['name']);
- $extension=$fileinfo['extension'];
- if(preg_match('/'.str_replace(',','|',$upext).'/i',$extension))
- {
- $filesize=filesize($temppath);
- if($filesize > $maxattachsize)$err='文件大小超过'.$maxattachsize.'字节';
- else
- {
- switch($dirtype)
- {
- case 1: $attach_subdir = 'day_'.date('ymd'); break;
- case 2: $attach_subdir = 'month_'.date('ym'); break;
- case 3: $attach_subdir = 'ext_'.$extension; break;
- }
- $attach_dir = $attachdir.'/'.$attach_subdir;
- if(!is_dir($attach_dir))
- {
- @mkdir($attach_dir, 0777);
- @fclose(fopen($attach_dir.'/index.htm', 'w'));
- }
- php_version < '4.2.0' && mt_srand((double)microtime() * 1000000);
- $filename=date("ymdhis").mt_rand(1000,9999).'.'.$extension;
- $target = $urldir.'/'.$attach_subdir.'/'.$filename;
- move_uploaded_file($upfile['tmp_name'],$target);
- if($immediate=='1')$target='!'.$target;
- $msg=str_replace('../',"",$target);
- }//开源代码Vevb.com
- }
- else $err='上传文件扩展名必需为:'.$upext;
- @unlink($temppath);
- }
- return array('err'=>$err,'msg'=>$msg);
- }
- ?>
- </body>
- </html>
新闻热点
疑难解答