dongya6381 2015-06-30 11:25
浏览 49

重命名文件名并立即读取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.

  • 写回答

1条回答 默认 最新

  • dongqie4233 2015-06-30 12:47
    关注
    $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;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答