Jump to content

Delete/Upload file in certain minutes


r0b

Recommended Posts

I've created a demo page for my CMS, and I want to delete itself and make a fresh content install.

 

I got the delete part figured out, but what about uploading fresh content files? (only a few text files).

 

Here's the delete part:

 

<?php
$expiretime=720; // minutes (in how many minutes it deletes the files)

$tmpFolder="tmp/"; // where to delete the files - be careful with this.
$fileTypes="*.*";

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

$FileCreationTime = filectime($Filename);
$FileAge = time() - $FileCreationTime; 

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

//deleting files:
unlink($Filename);
}

}
?>

Link to comment
Share on other sites

Just to make it more clear, the top code I pasted will delete files based on their last edited age. If the file hasn't been modified for more than XY minutes, it will be deleted.

 

In my case, if someone edited Home 5 minutes ago, it wont delete it.

 

Is there any way making it delete the files no matter what the last edit time was? (delete every 60 minutes, no matter what)

Link to comment
Share on other sites

I've found a better solution for deleting the files.

 

This works for deleting text files, if you want it to work for different types of files, please change .txt to something you need.)

 

<?php
  $path = dirname(__FILE__).'/files'; // directory where you want to delete your text files.
  if ($handle = opendir($path)) {

    while (false !== ($file = readdir($handle))) {
        if ((time()-filectime($path.'/'.$file)) < 86400) {  // 86400 = 60*60*24
          if (strripos($file, '.txt') !== false) {
            unlink($path.'/'.$file);
               }
   }
       }
   }
?>

 

All I need now is some kind of an auto upload to place the fresh files.

Link to comment
Share on other sites

Ok, I think this is pretty much achieved. The are two files, delete.php deletes all the text files in the specified folder and update.php re-creates the fresh ones I wanted to have.

 

There's NO actual auto delete or create, hoping someone will find a way.

 

Here's delete.php (deletes all text files from the folder).

 

<?php
  $path = dirname(__FILE__).'/files';
  if ($handle = opendir($path)) {

    while (false !== ($file = readdir($handle))) {
        if ((time()-filectime($path.'/'.$file)) < 86400) {  // 86400 = 60*60*24
          if (strripos($file, '.txt') !== false) {
            unlink($path.'/'.$file);
               }
   }
       }
   }
?>

 

And here's the update.php (creates one text file)

<?php

$filename = 'files/home.txt';

if (file_exists($filename)) {
    echo "The file $filename exists.";
} else {
    echo "The file $filename doesn't exist. Your file named $filename is now being created.";
}

$mydata = "This is my content.";

$myFile = "files/home.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $mydata);
fclose($fh);
?>

Link to comment
Share on other sites

New idea, combine them into one file for convenience. This will delete the files and create a new one at the same time.

<?php
  $path = dirname(__FILE__).'/files';
  if ($handle = opendir($path)) {

    while (false !== ($file = readdir($handle))) {
        if ((time()-filectime($path.'/'.$file)) < 86400) {  // 86400 = 60*60*24
          if (strripos($file, '.txt') !== false) {
            unlink($path.'/'.$file);
               }
   }
       }
   }
?>

<?php
$filename = 'files/home.txt';

if (file_exists($filename)) {
    echo "The file $filename exists.";
} else {
    echo "The file $filename was deleted.<br /> Your file named $filename was created.";
    echo "<br /><br />This will happen every time you refresh.";
}

$mydata = "This is my content.";

$myFile = "files/home.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $mydata);
fclose($fh);
?>

 

 

If you want to create multiple files at once, duplicate this code with different names. (Example shown below).

$mydata = "This is my content."; //change this ti $mydata2

$myFile = "files/home.txt"; //change this to $myFile2 and change the name of the text file.
$fh = fopen($myFile, 'w') or die("can't open file"); //thange $myFile to $myFile2
fwrite($fh, $mydata); //change this to mydata2
fclose($fh);

 

Still no idea how to make this do it automatically.

Link to comment
Share on other sites

You could either run the script as a cron job if your host allows it.

 

ORRR

 

A snippet of code like this on your interface pages could check the age of a static file you create when you reset the script, and do the process all over again one the file's last modification time is older than a set amount of time

 

<?php 

$file = 'somefile.txt';
$age = time() - 25; // 25 seconds old

if ( !file_exists($file) ) {
file_put_contents($file, 'This file was updated on '.date('l jS \of F Y h:i:s A'));
echo 'File was created.<br />';
}

if( filemtime($file) < $age ) {
file_put_contents($file, 'This file was updated on '.date('l jS \of F Y h:i:s A'));
echo 'File is older than $age. Updated file.<br />';
} else
echo 'File isn\'t old enough. No changes were made<br />';

echo file_get_contents($file);

?>

Link to comment
Share on other sites

Xyph, exactly what I was looking for, I have one question about this though.

 

If I understand correctly - if someone updated the file 2 seconds ago with some content testing the demo of this CMS, wouldn't refreshing the content just reset the demo?

 

On another notice, this is just  perfect, all I would have to do is change the time to 30 minutes and it would just reset when someone accessed the page at that time.

 

I would double mark this as solved. Thanks for another great simple solution Xyph.

 

If anyone uses this, don't worry about the $age displaying like this:

 

File is older than $age. Updated file.

 

If you echo $age you'll see it works eitherway.

 

Cheers and a sincere Thank You.

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.