PHP中文件名的关键字搜索

Posted

技术标签:

【中文标题】PHP中文件名的关键字搜索【英文标题】:Keyword search of file names in PHP 【发布时间】:2013-10-01 16:38:20 【问题描述】:

在另一个编码员的帮助下,我有 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><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>";
        
    
   




?>

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

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

提前致谢。

【问题讨论】:

【参考方案1】:
if ($extention[1] != "")    

    if(stripos($file, $keyword) !== false)
    ...
    

见 (stripos() manual)

【讨论】:

您是否建议将我的注释文本所在的位置以及 ... 的所有内容保持原样,然后将最后一个括号放在后面的“else”命令之前? 这应该替换if ($extention[1] != "") 行,你必须相应地放置关闭 以避免语法错误。 我插入了它,它没有做任何事情。搜索后,它什么也没找到,所以什么也没有显示(是的,我确保使用现有图像中的搜索词)。 在新的if 之前的var_dump($file);var_dump($keyword); 能给你带来什么? 当我输入一个关键字(“boy”)时,我得到:bool(false) string(3) "boy"【参考方案2】:

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

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>";
                
            
           

    

【讨论】:

以上是关于PHP中文件名的关键字搜索的主要内容,如果未能解决你的问题,请参考以下文章

linux下文件搜索

PHP怎么实现检索文件内容中存在关键字的文件?

百度高级搜索语法

php 活动日历:从Tribe栏中删除“搜索”/“关键字”字段。

php 活动日历:从Tribe栏中删除“搜索”/“关键字”字段。

php 活动日历:从Tribe栏中删除“搜索”/“关键字”字段。