php檔案上傳簡單一實例

來源:互聯網
上載者:User
 代碼如下 複製代碼

<!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(!empty($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(empty($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);
    }
   }
   else $err='上傳副檔名必需為:'.$upext;

   @unlink($temppath);
 }
 return array('err'=>$err,'msg'=>$msg);
}
?>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.