PHP限制檔案上傳的類型

來源:互聯網
上載者:User

PHP限制檔案上傳類型,如下代碼:

 
  1. <html> 
  2. <head> 
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
  4. <title>限制上傳檔案的類型 - www.cxybl.com</title> 
  5. <style type="text/css"> 
  6. <!-- 
  7. body { 
  8.     margin-left: 00px; 
  9.     margin-top: 0px; 
  10.     margin-right: 0px; 
  11.     margin-bottom: 0px; 
  12. --> 
  13. </style></head> 
  14.  
  15. <body> 
  16. <table width="385" height="185" border="0" cellpadding="0" cellspacing="0"> 
  17.   <tr> 
  18.     <td width="130" height="88"> </td> 
  19.     <td width="200"> </td> 
  20.     <td width="55"> </td> 
  21.   </tr> 
  22.   <form name="form1" method="post" action="index_ok.php" enctype="multipart/form-data"> 
  23.   <tr> 
  24.     <td height="26"> </td> 
  25.     <td align="center" valign="middle"> 
  26.     <input name="file4" type="file" id="file4" size="15" maxlength="150"></td> 
  27.     <td> </td> 
  28.   </tr> 
  29.   <tr> 
  30.     <td height="30" align="right"> </td> 
  31.     <td align="center" valign="top"><input type="submit" name="Submit" value="提交">   </td> 
  32.     <td> </td> 
  33.   </tr> 
  34.   </form> 
  35.   <tr> 
  36.     <td height="41"> </td> 
  37.     <td> </td> 
  38.     <td> </td> 
  39.   </tr> 
  40. </table> 
  41. </body> 
  42. </html> 

conn.php:

 
  1. <?php  
  2. $id=mysql_connect('localhost','root','root'); 
  3. mysql_select_db("db_database12",$id); 
  4. mysql_query("set names gb2312"); 
  5. ?> 

index_ok.php:

 
  1. <?php  
  2. session_start();  
  3. include("conn.php"); 
  4. ?> 
  5. <?php 
  6.  if($Submit=="提交"){ 
  7. $data=date("Y-m-d"); 
  8. $file_name="files";  //給上傳檔案命名 
  9. $name=$_FILES['file4']['name']; //擷取用戶端機器原檔案的名稱 
  10. $type=strstr($name,"."); //擷取從"."到最後的字元 
  11. if($type!=".txt"){ 
  12. echo "對不起,您上傳檔案的格式不正確!!"; 
  13. echo "<meta http-equiv=\"Refresh\" content=\"3;url=index.php?lmbs=檔案上傳\">將在3秒鐘後返回前頁..."; 
  14. }else{ 
  15.     $query="insert into tb_file1(file_name,file_text,data)values('$file_name','$file4','$data')"; 
  16.     $result=mysql_query($query); 
  17.     if($result=true){  
  18.     echo "上傳成功!!"; 
  19.     echo "<meta http-equiv=\"Refresh\" content=\"3;url=index.php?lmbs=檔案上傳\">";  
  20.     }else{echo "檔案上傳失敗!!"; 
  21.           echo "<meta http-equiv=\"Refresh\" content=\"3;url=index.php?lmbs=檔案上傳\">";} 
  22. }} 
  23. ?> 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.