重命名文件名并立即读取php中存在问题的文件

I have pdf files which are report cards of students.The report card names format is <student full name(which can have spaces)><space><studentID>.I need to download files.For this I have used the following code.

if(file_exists($folder_path.'/') && is_dir(folder_path)) { 
    $report_files = glob(folder_path.'/*'.'_*\.pdf' ); 
    if(count($report_files)>0)
    {
        $result_data = '';
        $result_data = rename_filenamespaces($report_files);
        var_dump($result_data);//this shows the edited filename

        foreach ($result_data as $file) { 
            if (strpos($file,$_GET['StudentID']) !== false) {
                 //code for showing the pdf docs to download
            }
        }
    }
}

//function for renaming if filename has spaces
function rename_filenamespaces($location)
{
    $new_location = $location; 
    foreach ($location as $file) {
       //check file has spaces and filename has studentID
       if((strpos($file," ")!==false)&& (strpos($file,$_GET['StudentID']) !== false))
       {
           $new_filename = str_replace(" ","-",$file);
           rename($file,$new_filename);
           $new_location = $new_filename;
       }
    }
    return $new_location;
}

The variable $result_data gives me the filename without spaces,but the for each loop is showing Warning:Invalid argument supplied for foreach(). But the filename is changed in the server directory immediately after running the function. This warning shows only for first time. I am unable to solve this.

$new_location = $new_filename;

$new_location is a array

$new_filename is a string

You have to use $new_location[$index]

or try

foreach ($new_location as &$file) {
...
...
$file = $new_filename;