首页 文章

PHP中关键字搜索文件名

提问于
浏览
1

在另一个编码人员的帮助下,我有PHP代码,可以创建一个照片库,其中包含目录中任何图像文件的自动创建缩略图 . (它是开源的,所以其他任何人都可以随意使用和修改 . )

我将它用作库存照片库,文件名包含该图像的关键字 . 例如,一个可能是 businessman-tie-suit-briefcase-office-meeting.jpg .

我一直在尝试添加一个查看文件名的关键字搜索输入,但无法弄清楚如何继续 . 我已经为数据库构建了关键字搜索,但这个目录文件名搜索对我来说是新的 .

这是页面的相关代码:

<?
$gallery = $_GET["gallery"];
$dir = $dir.$gallery."/";

//Put files into an array
// create a handler to the directory
$dirhandler = opendir($dir);

// read all the files from directory
$nofiles=0;
while ($file = readdir($dirhandler)) {

    // if $file isn't this directory or its parent 
    //add to the $files array
    if ($file != '.' && $file != '..')
        {
            $nofiles++;
            $files[$nofiles]=$file;                
        }   
    }

//close the handler
closedir($dirhandler);

//Back button to appear at the top of each page to go to the previous directory if not on the main page
if ($gallery !="" or $keyword !="") 
    {
        echo "<div><a href='javascript:history.go(-1)'><img src='images/up.png' border='0'></a></div>";
    }

// BEGINNING ADD FOR KEYWORD SEARCH 
// KEYWORD SEARCH BOX- create text box to search entire directory for that keyword and display page in grid pattern with results
?>
    <div style='position:relative; left:10px;'>
        <form action='index_search.php?keyword=$keyword' method='get' name='search'>
            <input type='text' name='keyword' size='25'>
            <input type='submit' value='Search Keyword'>
        </form> 
    </div>

<?  

//*************************************************************************//
// PERFORM KEYWORD SEARCH
if ($keyword !="") 
{
    echo "<div class='keytext'>Keyword search: <b>" . $keyword . "</b>
</div>"; /*********************************************************************************************************/ /* ***** THIS IS WHERE THE SEARCH OF DIRECTORY FILES NEEDS TO OCCUR AND OUTPUT RESULTS AS $files */ /* get results where $file LIKE %$keyword%; */ /*********************************************************************************************************/ //Show images foreach ($files as $file) { if ($file!="."&&$file!="..") { $extention = explode('.', $file); if ($extention[1] != "") { echo "<div class='imgwrapper'>"; echo"<a class='fancybox' rel='group'' href='$dir$file' return false' title='$filename'>"; echo "<img src='timthumb.php?src=$dir$file&h=$height&w=$width' alt='$extention[0]' width='$width' height='$height'/>"; echo"</a>
"; $file_name = current(explode('.', $file)); echo substr($file_name,0,21); echo "</div>"; } } } } else { // starts the split from keyword or no keyword //***********************************************************************// // sort folder names alphabetically, ignore case natcasesort($files); //Show the folders foreach ($files as $file){ if ($file!="."&&$file!="..") { sort($files); //Sorts the array (file names) alphabetically -- not the directory/folder names $extention = explode('.', $file); if ($extention[1] == "") { echo "<div class='imgwrapper'>"; echo "<a href='?gallery=$gallery/$file'>"; echo "<img src='images/folder.jpg' border='0'>"; echo "<div class='folder'>"; echo current(explode('.', $file)); echo "</a>"; echo "</div>"; echo "</div>"; } } } ?> <div style="clear:both"></div> <? //Show images foreach ($files as $file){ if ($file!="."&&$file!="..") { $extention = explode('.', $file); if ($extention[1] != "") { echo "<div class='imgwrapper'>"; echo"<a class='fancybox' rel='group'' href='$dir$file' return false' title='$filename'>"; echo "<img src='timthumb.php?src=$dir$file&h=$height&w=$width' alt='$extention[0]' width='$width' height='$height'/>"; echo"</a>
"; echo "<div class='title'>"; $file_name = current(explode('.', $file)); echo substr($file_name,0,125); echo "</div>"; echo "</div>"; } } } } ?>

我将该区域注释掉,我认为搜索字符串将被执行,因为显示代码已经存在 . 我尝试了一些不起作用的东西,所以没有打扰列出任何一个 .

如果我以错误的方式解决这个问题,有什么想法?

提前致谢 .

2 回答

  • 0

    我能够通过一些外部帮助解决这个问题 .

    if ($file!="."&&$file!="..") 
    {
        sort($files); //Sorts the array (file names) alphabetically -- not the directory/folder names
        $extention = explode('.', $file);
        if ($extention[1] == "")   
        {       
            $dir = "pics/";
            $dir = $dir.$file."/";
            $dirhandler = opendir($dir);
    
            // read all the files from directory
            $nofiles=0;
            $files2=array();
            while ($file2 = readdir($dirhandler)) {
                if ($file2 != '.' && $file2 != '..')
                    {
                        $nofiles++;
                        $files2[$nofiles]=$file2;                
                    }   
                }
            closedir($dirhandler);
            sort($files2); //Sorts the array (file names) alphabetically -- not the directory/folder names
    
            //Show images
            foreach ($files2 as $file2){   
                if ($file2!="."&&$file2!="..")
                {
                    $extention = explode('.', $file2);
                    if ($extention[1] != "" && stripos($file2,$keyword)!==false)
                    {       
                        echo "<div class='imgwrapper'>";
                        echo"<a class='fancybox' rel='group'' href='$dir$file2' return false' title='$filename'>";
                        echo "<img src='timthumb.php?src=$dir$file2&h=$height&w=$width' alt='$extention[0]' width='$width' height='$height'/>";
                        echo"</a>
    "; echo "<div class='title'>"; $file2_name = current(explode('.', $file2)); echo substr($file2_name,0,125); echo "</div>"; echo "</div>"; } } } }
  • 0
    if ($extention[1] != "")    
    {
        if(stripos($file, $keyword) !== false)
        {...
        }
    }
    

    见(stripos() manual

相关问题