Jump to content

unlink files that are older than one day (24hrs)


dk4210

Recommended Posts

Hi guys,

 

I using the following code

 

if ($handle = opendir('temp_photo')) {

 

    while (false !== ($file = readdir($handle))) {

        $filelastmodified = filemtime($file);

 

        if(($filelastmodified-time()) > 24*3600)

        {

          unlink($file);

        }

 

    }

 

    closedir($handle);

}

 

I want it to look in the directory and remove all the files that are older than 24 hours old.

 

When I run the code I get the following error..

 

Warning: filemtime() [function.filemtime]: stat failed for image1.jpg

Warning: filemtime() [function.filemtime]: stat failed for image2.jpg

Warning: filemtime() [function.filemtime]: stat failed for image3.jpg

Warning: filemtime() [function.filemtime]: stat failed for image4.jpg

 

It seems to read the directory, but it doesn't remove them and I receive the error

message.

 

Any ideas of what may be going on or do you have a script that may resolve this issue?

 

Thanks, Dan

Link to comment
Share on other sites

readdir returns the file name, not including the file path. Therefore $file alone doesn't exist. You'd need to use: 'temp_photo/' . $file .. glob I reckon would be better here though (as it does return the path):

 

foreach (glob('./temp_photo/*') as $file)
{
    $filelastmodified = filemtime($file);

    if(($filelastmodified-time()) > 24*3600)
    {
       unlink($file);
    }
}

 

Not tested but should work.

Link to comment
Share on other sites

I would rather do it in the code as opposed to the cron job

 

I am also trying this code, but it doesn't seem to work either

 

// Define the folder to clean

// (keep trailing slashes)

$checktfolder  = 'temp_photo/';

 

// Filetypes to check (you can also use *.*)

foreach (glob("temp_photo/*.*") as $fileTypes);

// $fileTypes = $filetypes2;

 

// Here you can define after how many

// minutes the files should get deleted

$expire_time    = 2;

 

// Find all files of the given file type

foreach (glob($checktfolder . $fileTypes) as $Filename) {

 

    // Read file creation time

    $FileCreationTime = filectime($Filename);

 

    // Calculate file age in seconds

    $FileAge = time() - $FileCreationTime;

 

    // Is the file older than the given time span?

    if ($FileAge > ($expire_time * 60)){

 

        // Now do something with the olders files...

 

print "The file $Filename is older than $expire_time minutes\n";

 

        // For example deleting files:

        unlink($Filename);

    }

 

}

Link to comment
Share on other sites

This may not help at all but its worth throwing out there.

 

Upon uploading the image I would have to saved to a DB with 3 rows:

 

Id    Name    Date

 

then in your script you can just check which are older then a day and unlink them by name

Link to comment
Share on other sites

are you sure your if statement ever evaluates to true? Make sure that is happening before looking as to whether the files are being deleted.

 

This will never evaluate to true since $filelastmodified - time() will always be a negative number.

if(($filelastmodified-time()) > 24*3600)

    {

      unlink($file);

    }

 

 

Link to comment
Share on other sites

Thanks guys for the expert help.. I finally got it to work with the following code

 

$expiretime=3600; //expire time in minutes

 

$tmpFolder="temp_photo/";

$fileTypes="*.*";

 

foreach (glob($tmpFolder . $fileTypes) as $Filename2) {

echo "$Filename2<br>";

 

// Read file creation time

$FileCreationTime = filectime($Filename2);

 

// Calculate file age in seconds

$FileAge = time() - $FileCreationTime;

 

// Is the file older than the given time span?

if ($FileAge > ($expiretime * 60)){

 

// Now do something with the olders files...

 

//print "The file $Filename2 is older than $expire_time minutes\n";

 

//deleting files:

unlink($Filename2);

}

 

}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.