drrvnbwle80177811 2012-03-23 10:06
浏览 166
已采纳

下载后删除文件 - PHP

I am working on Moodle 2.2.1 and have created dynamic.csv files using certain queries. I have created a /tmp/ directory with write permissions in my home directory and using file functions I have created a .csv file thru my code.

Everything works fine with the normal fopen(), fwrite() functions and csv files get created every time dynamically. I have kept these files for downloading for users with this piece of code.

<a href='/moodle/tmp/'".$filename."'> Download </a>

N with this line in .htaccess, the files are downloadable to any one's machine.

 AddType application/octet-stream .csv

But the problem right now is, every time I load the page, same .csv files get created with different timestamps. Basically I get a lot many duplicate files with different timestamps in my /tmp/ directory.

Is there a way that on every load of that page, all the existing instances of the files in that folder get deleted and a fresh batch of files are ready for download. So that I do not have duplicate files and just one instance of each file.

If any one can help me with this, I would really appreciate.

Thanks

EDIT: If my code for creating and writing a .csv file is this, then where should I delete the old files with timestamp before an hour and how to retain newest files.

 foreach($uniquenames as $uniquename)
 {
$data = "Header1,Header2,Header3,Header4,Header5
";
$filename = $uniquename.'_'.time().'.csv';
$writepath = $filepath.$filename;
$fh       = fopen($writepath, 'w');
$result = $functions->getFiless();
foreach($result as $activity)
{
    if($uniquename == $activity->quiz)
    {
        $data .= .$somedata.",".$foreachheader."
";        
    }
    else
    {

    }
}
fwrite($fh, $data);
fclose($fh);
echo "<p>".$uniquename . "<div class='assessment-reportlink'><a href='/moodle/tmp/".$filename."'> Download </a></p></div>";

}

  • 写回答

4条回答 默认 最新

  • dougu5847 2012-03-23 10:12
    关注

    What you basically need to do is:

    • Scan the directory with scandir()
    • Loop over it and check file creation times with filemtime() to find the 'newest file'
    • Delete all the files with unlink() that have older timestamps

    But do remember that sometimes it is a good idea to keep files with older timestamps. What if a link exists to one of the older files by a user running in parallel session? I suggest not deleting files based on what is 'newest', but based on time.

    Perhaps delete every file that is older than one hour and that is not the newest file.

    EXAMPLE OF DELETING ALL FILES EXCEPT ONE WITH THE NEWEST TIMESTAMP:

    // This is where I would scan the files from
    $directory='./mycsv/';
    
    // This scans the files
    $files=scandir($directory);
    
    // I will store the 'newest' timestamp here
    $newestFileModifiedTime=0;
    
    // Making sure that there are files
    if(!empty($files)){
        foreach($files as $file){
            // This ignores all current and parent directory references
            if($file!='.' && $file!='..'){
                // This ignores a directory, if that folder has other folders in it
                if(!is_dir($directory.$f)){
                    $fileModifiedTime=filemtime($directory.$file);
                    if($fileModifiedTime>$newestFileModifiedTime){
                        $newestFileModifiedTime=$fileModifiedTime;
                    }
                }
            }   
        }
        // We loop again since we found a file timestamp
        if($newestFileModifiedTime!=0){
            foreach($files as $file){
                // This ignores all current and parent directory references
                if($file!='.' && $file!='..'){
                    // This ignores a directory, if that folder has other folders in it
                    if(!is_dir($directory.$f)){
                        // This deletes all the files that are not with the newest timestamp
                        if(filemtime($directory.$file)!=$newestFileModifiedTime){
                            // Deleting the file
                            unlink($directory.$file);
                        }
                    }
                }   
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况