php文件上传简单实例

txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid,jar,jad,exe,html,htm,css,js,doc上传,音乐文件等都可以,实例代码如下:

  1. <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  5. <title>php文件上传</title>
  6. </head>
  7. <body>
  8. <form action="uploadsir.php" method="post" enctype="multipart/form-data"><input name="filedata" type="file" />
  9. <input type="submit" name="submit" value="上传" /></form>
  10. <?php
  11. if(!$_post)die();
  12. $state=uploadfile('filedata');
  13. if($state['err']){
  14. die('<script>alert("上传出错:'.$state['msg'].'");history.go(-1);</script>');
  15. }
  16. echo'<object type="application/x-shockwave-flash" data="template/images/copy.swf?u='.weburl.$state['msg'].'" width="100" height="40">
  17. <param name="movie" value="copy.swf?u='.weburl.$state['msg'].'" />';
  18. function uploadfile($inputname)
  19. {
  20. $immediate=$_get['immediate'];
  21. $attachdir='../pictures';//上传文件保存路径,结尾不要带/
  22. $urldir="../pictures";
  23. $dirtype=2;//1:按天存入目录 2:按月存入目录 3:按扩展名存目录 建议使用按天存
  24. $maxattachsize=2097152;//最大上传大小,默认是2m
  25. $upext='txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid,jar,jad,exe,html,htm,css,js,doc';//上传扩展名
  26. $err = "";
  27. $msg = "";
  28. $upfile=$_files[$inputname];
  29. if(!emptyempty($upfile['error']))
  30. {
  31. switch($upfile['error'])
  32. {
  33. case '1':
  34. $err = '文件大小超过了php.ini定义的upload_max_filesize值';
  35. break;
  36. case '2':
  37. $err = '文件大小超过了html定义的max_file_size值';
  38. break;
  39. case '3':
  40. $err = '文件上传不完全';
  41. break;
  42. case '4':
  43. $err = '无文件上传';
  44. break;
  45. case '6':
  46. $err = '缺少临时文件夹';
  47. break;
  48. case '7':
  49. $err = '写文件失败';
  50. break;
  51. case '8':
  52. $err = '上传被其它扩展中断';
  53. break;
  54. case '999':
  55. default:
  56. $err = '无有效错误代码';
  57. }
  58. }
  59. elseif(emptyempty($upfile['tmp_name']) || $upfile['tmp_name'] == 'none')$err = '无文件上传';
  60. else
  61. {
  62. $temppath=$upfile['tmp_name'];
  63. $fileinfo=pathinfo($upfile['name']);
  64. $extension=$fileinfo['extension'];
  65. if(preg_match('/'.str_replace(',','|',$upext).'/i',$extension))
  66. {
  67. $filesize=filesize($temppath);
  68. if($filesize > $maxattachsize)$err='文件大小超过'.$maxattachsize.'字节';
  69. else
  70. {
  71. switch($dirtype)
  72. {
  73. case 1: $attach_subdir = 'day_'.date('ymd'); break;
  74. case 2: $attach_subdir = 'month_'.date('ym'); break;
  75. case 3: $attach_subdir = 'ext_'.$extension; break;
  76. }
  77. $attach_dir = $attachdir.'/'.$attach_subdir;
  78. if(!is_dir($attach_dir))
  79. {
  80. @mkdir($attach_dir, 0777);
  81. @fclose(fopen($attach_dir.'/index.htm', 'w'));
  82. }
  83. php_version < '4.2.0' && mt_srand((double)microtime() * 1000000);
  84. $filename=date("ymdhis").mt_rand(1000,9999).'.'.$extension;
  85. $target = $urldir.'/'.$attach_subdir.'/'.$filename;
  86. move_uploaded_file($upfile['tmp_name'],$target);
  87. if($immediate=='1')$target='!'.$target;
  88. $msg=str_replace('../',"",$target);
  89. }//开源代码phpfensi.com
  90. }
  91. else $err='上传文件扩展名必需为:'.$upext;
  92. @unlink($temppath);
  93. }
  94. return array('err'=>$err,'msg'=>$msg);
  95. }
  96. ?>
  97. </body>
  98. </html>