2016-05-18 16 views
1

即時製作照片庫,我想使這顯示了所有在他們的名字所搜索的關鍵詞的圖像搜索欄的圖像。我已將我的圖像存儲在一個文件夾中(NOT DATABASE,IN FOLDER)。 我做了搜索欄,但我不能讓PHP的一部分工作。我需要ajax還是jQuery? 這是我當我按下搜索返回沒有當前的代碼。甚至沒有測試「eco'hello'」部分。如何搜索的文件夾爲包含在其名稱中的關鍵詞在PHP

?php 
$dir = "/uploads"; 
// Open a known directory, and proceed to read its contents 
if (is_dir($dir)) { 
    if ($dh = opendir($dir)) { 
     while (($file = readdir($dh)) !== false) { 
      if($file == $_POST['searching']){ 
// replace this line with eco <img> line 
       echo('<a href="'.$dir . $file.'">'. $file .'</a>'."\n"); 
       echo "hello" ; 
       } 
     } 
     closedir($dh); 
    } 
} 
?> 

我想獲得名稱中帶有關鍵字(搜索)的圖像列表。

+1

[水珠()](http://php.net/manual/en/function.glob .php)會輕鬆做到這一點 – 2016-05-18 04:32:45

回答

-1

這可能是與if (false !== strpos($file, $_POST['searching'])){更換if($file == $_POST['searching']){一樣簡單。

-1

你可以使用scandir()通過它來獲取目錄中的文件的數組,然後循環:

$dir_array=scandir($dir); 
foreach($dir_array as $file){ 
    if($file == $_POST['searching']){ 
    // open file etc. here 
    echo('<a href="'.$dir . $file.'">'. $file .'</a>'."\n"); 
    } 
} 
相關問題