douqie6454 2013-11-27 22:28
浏览 32
已采纳

php文件路径中的多个搜索值

I am working on a php site that needs to search a set of files with any combination of search fields.

The possible search fields are

id, year, building, lastname, firstname, birthdate

The folder structure and file names are as such

/year/building/file.pdf

The filenames contain the data to search

id_lastname_firstname_MM_dd_yy.pdf

I have everything working on the site except this part. Originally I only had ID, year, and building and I was able to do if's to check for every possibility of combinations. Now there is way more combinations so it much more complex.

I was thinking nested if and in_array or such, but there has to be a better way. I just learning my way around php.

I would like to be able to search with any combination of fields. I can change the filenames if it helps.

I started with something like this

function search($transcripts, $studentid=null, $year=null, $building=null, $last=null, $first=null, $birthdate=null){
$ext = '.pdf';
date_default_timezone_set('America/Los_Angeles');

$dir_iterator = new RecursiveDirectoryIterator("../transcripts");
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);

foreach ($iterator as $file) {
    if ($file->isFile()){
        $path = explode('\\',$file->getPath());
        $fname = explode('_', $file->getBasename($ext));
        if($path[1] == $year){
            if($path[2] == $building){
                if(in_array($last, $fname, true)){
                    if((in_array($first, $fname, true)){
                        if((in_array($birthdate

Originally I had seperate functions depending on which fields where filed in.

function bldStuSearch($building, $studentid, $transcripts){
$ext = '.pdf';
date_default_timezone_set('America/Los_Angeles');

$dir_iterator = new RecursiveDirectoryIterator("../transcripts");
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);

foreach ($iterator as $file) {
        $results = explode('\\',$file->getPath());
        //var_dump($results);
    if (($file->isFile()) && ($file->getBasename($ext)==$studentid) && ($results[2] == $building)){
        //echo substr($file->getPathname(), 27) . ": " . $file->getSize() . " B; modified " . date("Y-m-d", $file->getMTime()) . "
";
        $results = explode('\\',$file->getPath());
        //var_dump($results);
        //$building = $results[2];
        $year = $results[1];
        //$studentid = $file->getBasename($ext);
        array_push($transcripts, array($year, $building, $studentid));
        //var_dump($transcripts);
        //$size += $file->getSize();
        //echo '<br>';
    }
}
//echo "
Total file size: ", $size, " bytes
";
if (empty($transcripts))
{
    header('Location: index.php?error=2'); exit();
}

return $transcripts;

}

Now I am trying to have one search function to check for any combination? Any idea that would at least put in the right direction?

Thanks.

  • 写回答

1条回答 默认 最新

  • dongtan7351 2013-12-06 18:59
    关注

    So I had an idea about doing a scoring system but then dismissed it. I came back to it and found a way to make it work using a weighted scoring system.

    This allows the search to be super flexible and maintain being portable, not requiring a database for the metadata and using the filename as the search data without having to search each PDF. I am using A-Pdf splitter to split the PDF into separate files and add the metadata to the filename.

    I hope someone some day finds this useful for other searches. I am really happy the way this turned out.

    I will post the entire code when I am done on http://github.com/friedcircuits

    One thing I should change is to use named keys for the arrays.

    Here is the resulting code. Right now the birthdate has to be entered as m-d-yyyy to match.

    function search($transcripts, $studentid=null, $year=null, $building=null, $last=null, $first=null, $birthdate=null){
    $ext = '.pdf';
    $bldSearch = false;
    date_default_timezone_set('America/Los_Angeles');
    
    if (($building == null) AND ($year == null)){ $searchLocation = "../transcripts";}
    elseif (($year != null) AND ($building != null)){$searchLocation = "../transcripts/".$year."/".$building;}
    elseif ($year != null) {$searchLocation = "../transcripts/".$year;}
    elseif ($building != null) {
        $searchLocation = "../transcripts/";
        $bldSearch = true;
    }
    else{$searchLocation = "../transcripts";}
    
    $dir_iterator = new RecursiveDirectoryIterator($searchLocation);
    $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
    
    $score = 0;
    
    foreach ($iterator as $file) {
        if ($file->isFile()){
            //Fix for slashes changing direction depending on search path
            $path = str_replace('/','\\', $file->getPath());
    
            $path = explode('\\',$path);
            $fname = explode('_', $file->getBasename($ext));
            //var_dump($path);
            //echo "<br>";
            //var_dump($fname);
            //echo "<br>";
    
            //fix for different search paths
            if($path[1] == "transcripts"){
                $pYear = $path[2];
                $pbuilding = $path[3];
            }
            else{
                $pYear = $path[1];
                $pbuilding = $path[2];
            }
    
            if ($bldSearch == true){
                if ($building != $pbuilding) {continue;}
            }
    
    
            //$fname[1] = @strtolower($fname[1]);
            //$fname[2] = @strtolower($fname[2]);
    
    
            if($fname[0] == $studentid){
                $yearS = $pYear;
                $buildingS = $pbuilding;
                $studentidS = $fname[0];
                $lastS = $fname[1];
                $firstS = $fname[2];
                $birthdateS = $fname[3];
                array_push($transcripts, array($yearS, $buildingS, $studentidS, $lastS, $firstS, $birthdateS));
                continue;
            }
            if($pYear == $year){
                $score += 1;
            }
            if($path[2] == $building){
                $score += 1;
            }
            if(@strpos(@strtolower($fname[1]),$last) !== false){
                $score += 3;
            }
            if(@strpos(strtolower($fname[2]), $first) !== false){
                $score += 3;
            }
            if($fname[3] == $birthdate){
                $score += 3;
            }
            //echo $score." ";
            if ($score > 2) {
                $yearS = $pYear;
                $buildingS = $pbuilding;
                $studentidS = $fname[0];
                $lastS = $fname[1];
                $firstS = $fname[2];
                $birthdateS = $fname[3];
                array_push($transcripts, array($yearS, $buildingS, $studentidS, $lastS, $firstS, $birthdateS)); 
                //var_dump($transcripts);
            }
        }
        $score = 0;
    }
    
    if (empty($transcripts))
    {
        header('Location: index.php?error=2'); exit();
    }
    
    
    return $transcripts;}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解