php簡單檔案上傳代碼

來源:互聯網
上載者:User

<html>
<head>
<title>filemanage</title>
</head>
<body>
<center>
<p><h2>檔案管理</h2></p><br>
<table border=1><tr size=18>
<td>檔案ID</td><td>檔案名稱</td><td>檔案大小</td><td>上傳時間</td><td>下載</td><td>刪除</td></tr>
<?php
include("conn.php");
//echo '<table border=1>';
$rs=$db->query("select * from filem order by f_id DESC");
$i=1;
while($row = $rs->fetch_assoc())      
{
 $size=$row['f_size']/1024;
echo "<tr><td>".$i++."</td><td>".$row['f_name']."</td><td>".number_format($size, 2, '.', '')."KB</td><td>".$row['f_date']."</td><td><a href=".$row['f_url']." target=_blank>下載</a></td><td><a href=del.php?id=".$row['f_id'].">刪除</a></td></tr>"; 
}
echo '</table>';
unset($rs);
$db->close();
?>
</center>
<br><br><hr><br>
<b><h2>uploadfile</h2></b>
<br>
<form enctype="multipart/form-data" action="" method="post">
選擇上傳檔案:<br><input name="userfile" type="file"><br>
<input type="submit" value="發送">
</form>
<?php
 if(!$_FILES["userfile"]["name"])  exit;
//echo $_FILES['userfile']['type'];
if ($_FILES['userfile']['error'] > 0)
  {
    echo 'Problem: ';
    switch ($_FILES['userfile']['error'])
    {
      case 1:  echo 'File exceeded upload_max_filesize';  break;
      case 2:  echo '不能超過800M';  break;
      case 3:  echo 'File only partially uploaded';  break;
      case 4:  echo 'No file uploaded';  break;
    }
    exit;
  }
else
{ //檢查上傳檔案是否在允許上傳的類型
   $tp = array("gif","jpeg","png","txt","doc","rar","zip","xls","bmp","wmv","mp3","flv","rmvb","avi");
if (!in_array(strtolower(substr(strrchr($_FILES['userfile']['name'], '.'),1)), $tp))

   {
    echo '檔案類型錯誤,請重新選擇檔案!<br>只允許rar,zip,jpg,gif,txt,png,bmp,xls類型的檔案';
    exit;
   }

   $path="./file/";        //上傳路徑

   if(file_exists($path.$_FILES['userfile']['name']))    //判斷檔案是否存在
   {
    echo '檔案已存在,請更改後重新上傳!';
    exit;
   }

   if($_FILES["userfile"]["name"])
   {
        $file1=$_FILES["userfile"]["name"];
        $file2 = $path.$file1;
        $flag=1;
   }
   if($flag)
       $result=move_uploaded_file($_FILES["userfile"]["tmp_name"],$file2);
   //特別注意這裡傳遞給move_uploaded_file的第一個參數為上傳到伺服器上的臨時檔案
   if($result)
      {
        
         $time=date("Y-m-d");
     //   $url=$patch.$name;
        $size=$_FILES["userfile"]["size"];
         include("conn.php");
         $rs=$db->query("insert into filem(f_name,f_url,f_date,f_size) values('$file1','$file2','$time','$size')");
    //     $rs=$db->query($sql);
       echo "<script language='javascript'>location='index.php';</script>";
      }
   unset($rs);
   $db->close();

}
?>
</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.