How to delete files from directory on specific time, daily? - php

My data.txt is generated each time when someone is visiting my site. I would like to delete this file on on specific time, let say 1:00AM daily.
I found this script but I'm struggling to update the code :/
Any advice?
<?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 (preg_match('/\.txt$/i', $file)) {
unlink($path.'/'.$file);
}
}
}
}
?>

The script you posted deletes .txt files in the given folder if they are older than a day but the problem is that this test only happens once - when you run the script.
What you need to do is run this script periodically. If you are running this on Linux you should probably add a cron job that executes this script periodically, say once an hour or once daily.
If you're running this on Windows, there is a task schedule that you could use to accomplish the same thing.

Use task scheduler, such as cron for this purposes. You can remove your file simply via shell command
rm /path/to/data.txt
So there's no need to write a PHP script for that.

Related

PHP Create file and delete it after one hour, one week etc

I want to create a file and can auto delete it after the necessary period.
My code is:
$timeForDelete=$_REQUEST['timeForDelete'];
$text=$_REQUEST["text"];
$filename = uniqid(rand(), true) . '.txt';
if($timeForDelete =="2"){
//how save text to file and auto delete file after one hour?
}
else{
$f=fopen($filename,'a');
fwrite($f,$text); //write to file
}
if $timeForDelete ==2 : how can I save text to file and auto delete it after one hour?
Hope you help solve this problem. Thanks.
There are a number of ways to do what you are requesting.
I would suggest that the quickest way to do this would be:
Creating the file with a name that is uniquely identifiable as
needing to be deleted (and based on your question, perhaps a timestamp in the name after which they can be deleted - One hour, one week, etc.)
Write a script that will delete all files containing that unique identifier based on the time they were created.
Set up a cronjob to run the script every 5 minutes and clean up your un-needed files.
Let me try to give an example script that the cron would run.
http://php.net/manual/en/function.filemtime.php
Unfortunately on Linux systems we can't get the file creation date, so fmtime is our best bet. For more info please read the wiki below fmtime's documentation.
Note: This is just a simple example
List all the files in a directory:(/path/to/your/script/yourscript.php)
//assuming files are stored in /path/to/your/script/myfiles
$path = realpath(dirname(__FILE__)) . '/myfiles';
$files = array_diff(scandir($path), ['.', '..']);
//assuming all your files are in $paths top level
foreach($files as $file){
//unix time
$ctime = fmtime($path . "/$file");
//basic math
if(time() > $ctime){
unlink($path . "/$file");
}
}
Your cron job
If you 're running a Unix like OS:
Runs every 5 minutes. You can reconfigure
crontab -e
Add this to the crontab and save:
#invoke the intepreter
*/5 * * * * php /path/to/your/script/yourscript.php
Edit:
Alternatively you can save the upload times of these files in a database to keep track of file creation dates
this code is 100% working
date_default_timezone_set('Asia/Kolkata');
$path = realpath(dirname(__FILE__)) . '\Register.php'; <'Enter Your delete file name'>
$creat_time = date("d-m-Y h:i:s",filemtime($path));
$endTime = strtotime("+1 year", strtotime($creat_time));
if(time()>$endTime){
unlink($path);
}

Temporary files that hang around after script execution

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.
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
$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

Automatically delete files based on date of creation

I am having issue with automatically deleting files from a specific folder on my server.
I need to run an automatic delete every 31 mins on a folder which stores incoming documents. These files will always be *.pdf format.
I have found an issue similar on this site.
How to delete files from directory based on creation date in php?
However my issue is with *.pdf files and I have never used php before, ideally I was looking for a .bat file, but if that's not possible it's no problem.
<?php
if ($handle = opendir('/path/to/files')) {
while (false !== ($file = readdir($handle))) {
$filelastmodified = filemtime($file);
if((time() - $filelastmodified) > 24*3600 && strtolower(substr($file, -4)) == ".pdf")
{
unlink($file);
}
}
closedir($handle);
}
?>
This added condition checks if the filename ends with ".pdf". You could run this as a cronjob.
You might as well use shell commands instead. find with -name, -exec and -mtime does the same and saves the need for a PHP parser.

Delete all files in directory automatically?

How does one, in PHP, delete all files in a directory every 24 hours without deleting the directory itself? It has to be in PHP, it cannot be a Cron Job.
Can you please provide an explanation behind your code as well? I am still learning PHP.
Thank you!
There is no way to do this in PHP without using PHP. Sorry.
Joking, but if you wanted to do this you would need some sort of task scheduler (like cron).
That is to say that you could program your personal computer to send the request to the server every 24 hours, but you would either have to do it manually or schedule the task locally.
My point being, you need cron, but it doesn't need to be running on the same host as the PHP files.
Without cron you'd have to add code like this to a commonly-requested page:
$scheduledclean = strtotime("midnight"); // 00:00:00 of the current day
$lastcleanfile = '/path/to/my/app/lastclean';
$lastcleantime = (file_exists($lastcleanfile)) ? filemtime($lastcleanfile) : 0;
$curtime = time();
if( ($curtime > $scheduledclean) && ($lastcleantime < $scheduledclean) ) {
touch($lastcleanfile); //touch first to prevent multiple executions
// file cleanup code here
}
On the first request to the page after midnight the cleanup will fire off, but the unlucky person that made the request will likely have a delay for their page to be served that is as long as the cleanup takes. You could mitigate this by running the cleanup as a backgrounded shell command like shell_exec('rm -rf /path/to/dir/* &');
I did something similar to this a long time ago. It's a terrible idea, but you can have a file which stores the last time your directory was cleared. Each time a user visits a relevant page, check this file in the PHP script (you could also check the modified time). If it is far enough into the past, update the file and run your delete script.
Downsides:
Not guaranteed to run every 24 hours (maybe you get no visitors one day)
Gives one user per day a longer wait
Ugly
As for deleting the files,
function delete_contents( $dirpath ) {
$dir = opendir( $dirpath );
if( $dir ) {
while( ($s = readdir( $dir )) !== false ) {
if( is_dir( $s ) ) {
delete_contents( $s );
rmdir( $s );
} else {
unlink( $s );
}
}
closedir( $dir );
}
}
BE VERY CAREFUL with that. On a crude server setup, delete_contents('/') will delete every file.
Make a PHP script that removes all files in the directory, look for the functions readdir() and unlink() to remove the files.
Set-up a Cron Job to run the script automatically each 24 hours. How you have to do this exactly depends on your host. There are also websites that you can use for this: http://www.google.nl/search?q=cronjobs+webservice
Good luck!

Delete log files automatically in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Automatically delete files from webserver
I'm logging some XML content to a text file using file_put_contents() in case I need it for debugging.
How do I delete this file after a certain amount of time?
I've got a restrictive .htaccess in the log folder, but I'd rather not leave the info (will have customer's addresses, etc) up on the web for long.
Well, while agreeing with everyone else that you're using the wrong tool for the job, your question is pretty straightforward. So here you go:
write a PHP script that will run from the command line
use a tool like cron or windows scheduled task
invoke the cron every minute/five minutes/etc
Your script would be pretty straightforward:
<?php
$dh = opendir(PATH_TO_DIRECTORY);
while( ($file = readdir($dh)) !== false ) {
if( !preg_match('/^[.]/', $file) ) { // do some sort of filtering on files
$path = PATH_TO_DIRECTORY . DIRECTORY_SEPARATOR . $file;
if( filemtime($path) < strtotime('-1 hour') ) { // check how long it's been around
unlink($path); // remove it
}
}
}
You could also use find if you're working in Linux, but I see that #Rawkode posted that while I was writing this so I'll leave you with his elegant answer for that solution.
You should be using the PHP error_log() function which will respect the php.ini settings.
error_log('Send to PHP error log');
error_log('Sent to my custom log', 3, "/var/tmp/my-errors.log");
You can use a built-in function – filectime – to track the creation date of your log files and then delete those that are old enough to be deleted.
This code searches through the logs directory and deletes logs that are 2 weeks old.
$logs = opendir('logs');
while (($log = readdir($logs)) !== false)
{
if ($log == '.' || $log == '..')
continue;
if (filectime('logs/'.$log) <= time() - 14 * 24 * 60 * 60)
{
unlink('logs/'.$log);
}
}
closedir($logs);
You should be handling your logging better, but to answer your question I'd use the *nix find command.
find /path/to/files* -mtime +5 -delete
This will delete all files that haven't been modified in five days. Adjust to your own needs.
There are two ways you can work it out:
if you write the log to only one file, you can empty the file using something like this:
<?php file_put_contents($logpath, "");
if you generate many files, you can write cleanup function like this:
<?php
$logpath = "/tmp/path/to/log/";
$dirh = opendir($logpath);
while (($file = readdir($dh)) !== false) {
if(in_array($file, array('.', '..'))) continue;
unlink($logpath . $file);
}
closedir($dirh);
You can set up a server Scheduled task (most known as Cron Job under *nix systems) to run on a regular basis and delete the left log files. The task would be to execute a PHP code that will do the job.
See Cron Job Overvieww

Categories