Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7680059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:01:19+00:00 2026-05-31T18:01:19+00:00

I am working on Moodle 2.2.1 and have created dynamic.csv files using certain queries.

  • 0

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>";

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:01:20+00:00Added an answer on May 31, 2026 at 6:01 pm

    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);
                        }
                    }
                }   
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working in Eclipse on a Dynamic Web Project (using Tomcat (v5.5) as the app
I am working with Moodle 2.2.1 but have a basic php-mysql problem. I am
Working on a website that has Employee and Branch entities, using a database table
I'm working on a new Moodle Assignment plugin. How can I include a custom
Working sample using one Table SELECT t.* FROM ( SELECT TITLE.name, (TITLE.value-TITLE.msp) AS Lower,
Working with a Lucene index, I have a standard document format that looks something
Working in Java: I have a JFrame class, and separate classes for my two
I am new to moodle ,currently i am working on questionnaire module, in which
I am currently working on a school system where we have a parent course
When working with some open source projects (in my case Joomla and Moodle), I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.