manu
manu

Reputation: 11

finding a file starting with a string inside a folder

$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

Upvotes: 0

Views: 636

Answers (1)

ghstcode
ghstcode

Reputation: 2912

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

Upvotes: 1

Related Questions