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\n";
$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."\n";
}
else
{
}
}
fwrite($fh, $data);
fclose($fh);
echo "<p>".$uniquename . "<div class='assessment-reportlink'><a href='/moodle/tmp/".$filename."'> Download </a></p></div>";
}
What you basically need to do is:
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: