当前位置:首页 > PHP教程 > php上传下载 > 列表

php文件上传简单实例

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-09 11:26:48 浏览: 评论:0 

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

Tags: php文件上传 php上传实例

分享到: