PHP限制圖片上傳的格式,如下代碼:
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
- <title>限制上傳圖片的格式 - www.cxybl.com</title>
- <style type="text/css">
- <!--
- .style1 {
- font-size: 14px;
- font-family: "華文行楷";
- }
- .style4 {font-size: 12px; font-weight: bold; }
- -->
- </style>
- </head>
-
- <body>
- <table width="406" height="129" border="0" align="center" cellpadding="0" cellspacing="0">
- <tr>
- <td width="106" height="40"> </td>
- <td width="196"> </td>
- <td width="31"> </td>
- </tr>
- <form name="form1" method="post" action="index_ok.php" enctype="multipart/form-data">
- <tr>
- <td height="32" align="right"><span class="style1">圖片路徑</span>:</td>
- <td valign="middle"><input name="images" type="file" id="images2" size="15">
- <input type="hidden" name="MAX_FILE_SIZE" value="30000"> </td>
- <td> </td>
- </tr>
- <tr>
- <td height="44" align="right" valign="middle"><span class="style4">圖片的格式</span>:</td>
- <td valign="middle"><span class="style4">(.jpg)</span>
- <input type="submit" name="Submit" value="提交"></td>
- <td> </td>
- </tr>
- </form>
- <tr>
- <td height="10"> </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");
- $path = 'upfiles/'. $_FILES['images']['name'];
- $type=strstr($path,'.');
- if($type!=".jpg"){echo "<script> alert('對不起,您上傳的圖片的格式不對!!'); history.back();</script>";
- }else{
- if (move_uploaded_file($_FILES['images']['tmp_name'],$path)) {
- $query1="insert into tb_image2(path,data,image_name)values('$path','$data','$images')";
- $result1=mysql_query($query1);
- if($result1=true){
- echo "上傳成功!!";
- echo "<meta http-equiv=\"Refresh\" content=\"3;url=index.php\">";
- }else{echo "檔案上傳失敗!!";
- echo "<meta http-equiv=\"Refresh\" content=\"3;url=index.php\">";}
- }}}
- ?>