返回列表 發帖

php檔案管理_圖片搜尋

  1. <html>
  2.     <head>
  3.         <title>檔案管理</title>
  4.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5.     </head>
  6.     <body>
  7.         <form name="form_search" action="" method="get" onsubmit="return checkForm();"><!-- HTML 註解 -->
  8.             請輸入要搜尋的圖片名稱:<input type="text" name="file_name" value="<?php echo $_GET['file_name'];?>" />
  9.             <input type="submit" name="send" value="開始搜尋" />
  10.         </form>
  11.         <script type="text/javascript">
  12.             //自定方法, 檢查圖片名稱欄位是否有值
  13.             function checkForm(){
  14.                 //取得 document(body) 裡的 form_search(form) 裡的 file_name(input) 的值(value)
  15.                 $file_name = document.form_search.file_name.value;
  16.                 if($file_name===""){
  17.                     alert("請填寫圖片名稱");
  18.                     return false;
  19.                 }else{
  20.                     return true;
  21.                 }
  22.             }
  23.         </script>
  24.         <?php
  25.             $scan_dir = 'photo'; //要讀取的資料夾
  26.             $arr_file = scandir($scan_dir);    //讀取資料夾, 並以陣列方式儲存
  27.             foreach($arr_file as $key=>$value){    //利用foreach迴圈讀取資料夾
  28.                 if($value=='.' || $value=='..'){    //過濾 .和.. 字串
  29.                     continue;
  30.                 }
  31.                 $file_path = $value;    //拼湊檔案路徑
  32.                 /*if(@substr_count($file_path, $_GET['file_name'])>=1){ //判斷路徑字串在搜尋的字串出現的次數, 如果出現一次以上就列印出來
  33.                     echo '<img src="'.$file_path.'" width="100px" />'.$value.'<br />';
  34.                 }*/
  35.                 if(stripos($file_path, $_GET['file_name'])!==false){ //判斷路徑字串在搜尋的字串第一次出現的次數, 若回傳不是false代表有值
  36.                     echo '<img src="'.$scan_dir.'/'.$file_path.'" width="100px" />'.$value.'<br />';
  37.                 }
  38.             }
  39.         ?>
  40.     </body>
  41. </html>
  42. </html>
複製代碼
May

返回列表