doudou201701 2015-09-06 09:43
浏览 76

查找以文件夹内的字符串开头的文件

$file = glob("submission/".$iid."/".$yr."/".$sem."/".$sec."/Word/".$sid.".*");
 if ($file!=="")
{ 
    return $path."/". $file;
   break;
}

the sid is the student submitted file starting with the student id .I want tyo find the exact file from inside the folder 'word' .The file is like 211000110-Word.docx

  • 写回答

1条回答 默认 最新

  • drema2014 2015-09-06 10:05
    关注

    glob() returns an array that you need to further loop through and match the individual values against the value you are looking for.

    $files = glob("submission/".$iid."/".$yr."/".$sem."/".$sec."/Word/*.docx");
    
    $result = false; 
    
    foreach($files as $file) {
        if($file == "$sid-Word.docx"){
            $result = $file;
            break;
        }
    }
    

    Turning this into a re-usable function would be a good idea:

    function get_student_file($sid) {
        $files = glob("submission/".$iid."/".$yr."/".$sem."/".$sec."/Word/*.docx");
    
        $result = false; 
    
        foreach($files as $file) {
            if($file == "$sid-Word.docx"){
                $result = $file;
                break;
            }
        }
    
        unset($files);
    
        return $result;
    }
    
    评论

报告相同问题?