Question

Can I create temporary files with php that expire and get deleted after a predefined time set during file creation, and not when the script finishes execution? My host doesn't allow cron jobs, so php only, if possible.

Était-ce utile?

La solution

Without access to cron you only have one option -- manage file cleanup on your own.

This is, in fact, what the PHP SESSION handler does -- it creates a file of session data. Then, when PHP starts, there is a small chance that it will go through an remove expired files. (IIRC, there is a 1/100 chance that it will.)

Your best bet is to create a directory to store your temp files in and then use a similar process.

This might give you some ideas: Session Configuration

Autres conseils

$deletetime = time() - $days * 86400; # only do this calculation
$dir = '/path/to/dir';
if ($handle = opendir($dir)) {
  while (false !== ($file = readdir($handle))) {
   if ((filetype("$dir/$file") == 'file') && (filemtime($file) < $deletetime)) {
    unlink("$dir/$file");
   }
  }
 closedir($handle);
}

reference - webhostingtalk

Also here is a similar question asked before.Auto delete files after x-time

Is this what you are looking for?

<?php
$fh = fopen('test.html', 'a');
fwrite($fh, '<h1>Hello world!</h1>');
fclose($fh);

unlink('test.html');
?>

http://www.php.net/manual/en/function.unlink.php

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top