PHP限制檔案上傳類型,如下代碼:
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
- <title>限制上傳檔案的類型 - www.cxybl.com</title>
- <style type="text/css">
- <!--
- body {
- margin-left: 00px;
- margin-top: 0px;
- margin-right: 0px;
- margin-bottom: 0px;
- }
- -->
- </style></head>
-
- <body>
- <table width="385" height="185" border="0" cellpadding="0" cellspacing="0">
- <tr>
- <td width="130" height="88"> </td>
- <td width="200"> </td>
- <td width="55"> </td>
- </tr>
- <form name="form1" method="post" action="index_ok.php" enctype="multipart/form-data">
- <tr>
- <td height="26"> </td>
- <td align="center" valign="middle">
- <input name="file4" type="file" id="file4" size="15" maxlength="150"></td>
- <td> </td>
- </tr>
- <tr>
- <td height="30" align="right"> </td>
- <td align="center" valign="top"><input type="submit" name="Submit" value="提交"> </td>
- <td> </td>
- </tr>
- </form>
- <tr>
- <td height="41"> </td>
- <td> </td>
- <td> </td>
- </tr>
- </table>
- </body>
- </html>
conn.php:
- <?php
- $id=mysql_connect('localhost','root','root');
- mysql_select_db("db_database12",$id);
- mysql_query("set names gb2312");
- ?>
index_ok.php:
- <?php
- session_start();
- include("conn.php");
- ?>
- <?php
- if($Submit=="提交"){
- $data=date("Y-m-d");
- $file_name="files"; //給上傳檔案命名
- $name=$_FILES['file4']['name']; //擷取用戶端機器原檔案的名稱
- $type=strstr($name,"."); //擷取從"."到最後的字元
- if($type!=".txt"){
- echo "對不起,您上傳檔案的格式不正確!!";
- echo "<meta http-equiv=\"Refresh\" content=\"3;url=index.php?lmbs=檔案上傳\">將在3秒鐘後返回前頁...";
- }else{
- $query="insert into tb_file1(file_name,file_text,data)values('$file_name','$file4','$data')";
- $result=mysql_query($query);
- if($result=true){
- echo "上傳成功!!";
- echo "<meta http-equiv=\"Refresh\" content=\"3;url=index.php?lmbs=檔案上傳\">";
- }else{echo "檔案上傳失敗!!";
- echo "<meta http-equiv=\"Refresh\" content=\"3;url=index.php?lmbs=檔案上傳\">";}
- }}
- ?>