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

}


?>

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

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

提前致谢。

4

2 回答 2

0
if ($extention[1] != "")    
{
    if(stripos($file, $keyword) !== false)
    {...
    }
}

参见(stripos() 手册

于 2013-10-01T16:57:04.167 回答
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><br/>";
                    echo "<div class='title'>";
                    $file2_name = current(explode('.', $file2));
                    echo substr($file2_name,0,125);
                    echo "</div>";
                    echo "</div>";
                }
            }
        }   

    }
于 2013-10-07T00:07:50.800 回答