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>";
}
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:
// 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);
}
}
}
}
}
}
Before you create the file iterate through existing files in that directory and delete what is not necessary.
Before file creation:
foreach (glob("*.csv") as $filename) {
unlink($filename);
}
this is works for me:
$file = $_GET['f'];
if ($file and file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
if (readfile($file))
{
unlink($file);
}
}