dongyoudi1342 2013-10-01 16:38
浏览 160
已采纳

PHP中关键字搜索文件名

With another coder's help, I have PHP code that creates a photo gallery with auto-created thumbnail images of any image files in the directory. (It's open source, so anyone else is free to use and modify as they desire.)

I use it as a stock photo library and the file names include the keywords for that image. For example, one might be businessman-tie-suit-briefcase-office-meeting.jpg.

I've been trying to add a keyword search input that looks at the file names, but cannot figure out how to proceed. I've built keyword searches for a database, but this directory file name search is new to me.

Here's the relevant code of the page:

    <?
$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><br/></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><br/>";
                    $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><br/>";
            echo "<div class='title'>";
            $file_name = current(explode('.', $file));
            echo substr($file_name,0,125);
            echo "</div>";
            echo "</div>";
        }
    }
}   

}


?>

I have the area commented out where I believe the search string would be executed, as the display code is already there. I've tried a few things that didn't work, so didn't bother listing any of it.

Any ideas if I'm going about this the wrong way?

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dou91808 2013-10-07 00:07
    关注

    I was able to get this resolved from some outside help.

    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><br/>";
                        echo "<div class='title'>";
                        $file2_name = current(explode('.', $file2));
                        echo substr($file2_name,0,125);
                        echo "</div>";
                        echo "</div>";
                    }
                }
            }   
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程