Avatar of Chiehkai
Chiehkai
Flag for Taiwan, Province of China asked on

File Deleting PHP Script

Does anyone know where I could find a PHP script that deletes files older than X hours in a specific folder? And if possible I could declare which files will never be deleted.

I think it should a simple one, but I couldn't find it anywhere.

Thanks.
PHP

Avatar of undefined
Last Comment
Chiehkai

8/22/2022 - Mon
Panchux

You may find the solution in this link

http://www.tizag.com/phpT/filedelete.php

In Windows environments you may try

<?php
chown($TempDirectory."/".$FileName,666);
unlink($TempDirectory."/".$FileName);
?>

to change permisions to grant deletion of the file.

Hope that helps,

Pancho
Chiehkai

ASKER
Thanks for the information. I checked the link you provided but it only mentioned how to delete a "specific" file with PHP, I hope the script could find files older than X hours and delete it for me.
flwam93

<?
$dir = '/home/****/public_html/cronstuff/';
$dp = opendir($dir) or die ('Could not open '.$dir);
while ($file = readdir($dp)) {
if ((eregi('testfile',$file)) && (filemtime($dir.$file)) < (strtotime('-10 days'))) {
unlink($dir.$file);
}
}
closedir($handle);
?>

Is this something that helps you?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
mosidiot

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Chiehkai

ASKER
Thanks for your script mosidiot, can we add a file (say like the .htaccess) that will never be deleted?
flwam93

if($filename == ".htaaccess") {
// Nothing here
}
else {
// Delete all other files
unlink("yourfile");
}
Chiehkai

ASKER
May I know what "eregi('testfile',$file)" does? Do I have to change 'testfile' to something else?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Chiehkai

ASKER
I get the following error while running the script :

Warning: filectime() [function.filectime]: stat failed for FILE_NAME in PATH_TO_FILE on line 38

I have replaced file name & path to file.

Thanks.
mosidiot

replace this line $diff=get_time_difference(time(),filectime($file));

with $diff=get_time_difference(time(),filectime($dir.$file));
Chiehkai

ASKER
Thanks, but now I get this error :

Parse error: syntax error, unexpected T_IF in PATH_TO_SCRIPT on line 39
Your help has saved me hundreds of hours of internet surfing.
fblack61
mosidiot

Hmm.. can you show me your line 37 to 39?
Chiehkai

ASKER
Sure, see below.
        if($file != "." && $file != "..") {
            $diff=get_time_difference(time(),filectime($dir.$file)) //getting the file created time
            if ($diff['hours']>1){ //if more than 1 hours of the created time, delete
                chmod($dir.$file, 0777);
                unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");
            }

Open in new window

mosidiot

lol.. replace this $diff=get_time_difference(time(),filectime($dir.$file)) //getting the file created time

with $diff=get_time_difference(time(),filectime($dir.$file)); //getting the file created time

Dude, you forget the semi colon

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Chiehkai

ASKER
Ah, my wrong. Not sure why it was removed.

There's no more errors now, but none of the files are deleted :(
Member_2_4694817

No files are being deleted and the script dies with a "couldn't delete ..." ?
Or the script thinks that no files need to be deleted? (To check add a debugging line between 3 and 4:
echo "Trying to delete $file<br />";

BTW, why do you chmod 0777 before unlining?
If the process has insufficient privileges to unlink the file, it will hardly have enough privileges to change permissions ...
Chiehkai

ASKER
There were completely no message output, but once I added your debug line it displayed :

Trying to delete
Trying to delete
Trying to delete
Trying to delete
Trying to delete
Trying to delete
Trying to delete

(No filenames after that message)

The total "Trying to delete" messages are same as the amount of files in that folder.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Member_2_4694817

If the filename is not shown there must be some typo. But at least the correct count gives us hope that we actually loop over the correct files.
What is the output of the following extreme-debugging version?


function destroy($dir) {
    echo "destroying directory '$dir' <br/>";
    $mydir = opendir($dir);
    while(false !== ($file = readdir($mydir))) {
        echo "readdir gives '$file' <br/>";
        if($file != "." && $file != "..") {
            $fullfile = $dir . $file;
            echo "full file name is '$fullfile' <br/>";
            $diff=get_time_difference(time(),filectime($fullfile));//getting the file created time
            $diffhours = $diff['hours'];
            echo "age of $fullfile is $diffhours hours<br/>";
            if ($diffhours>3){//if more than 3 hours of the created time, delete
                echo "trying to delete '$fullfile' <br/>";
                $ok = chmod($fullfile, 0777);
                echo "chomd --> $ok <br/>";
                $ok = unlink($fullfile);
                echo "unlink --> $ok <br/>";
            }
        }
    }
    closedir($mydir);
}

Open in new window

Chiehkai

ASKER
Thanks for the information, it listed all the files that should be deleted, but files just aren't really been deleted.

Below is the output of your extreme-debugging version :

destroying directory '/home/HIDDEN/public_html/uploads/'
readdir gives '.'
readdir gives '..'
readdir gives 'menu_footer.jpg'
full file name is '/home/HIDDEN/public_html/uploads/menu_footer.jpg'
Trying to delete
age of /home/HIDDEN/public_html/uploads/menu_footer.jpg is 0 hours
readdir gives 'Accelerometer Plugin.zip'
full file name is '/home/HIDDEN/public_html/uploads/Accelerometer Plugin.zip'
Trying to delete
age of /home/HIDDEN/public_html/uploads/Accelerometer Plugin.zip is 0 hours
readdir gives 'girl.jpg'
full file name is '/home/HIDDEN/public_html/uploads/girl.jpg'
Trying to delete
age of /home/HIDDEN/public_html/uploads/girl.jpg is 0 hours
readdir gives 'vb_reference_sheet.pdf'
full file name is '/home/HIDDEN/public_html/uploads/vb_reference_sheet.pdf'
Trying to delete
age of /home/HIDDEN/public_html/uploads/vb_reference_sheet.pdf is 0 hours
readdir gives '.htaccess'
full file name is '/home/HIDDEN/public_html/uploads/.htaccess'
Trying to delete
age of /home/HIDDEN/public_html/uploads/.htaccess is 0 hours
readdir gives 'vb_reference_sheet_1.pdf'
full file name is '/home/HIDDEN/public_html/uploads/vb_reference_sheet_1.pdf'
Trying to delete
age of /home/HIDDEN/public_html/uploads/vb_reference_sheet_1.pdf is 0 hours
readdir gives 'remover.php'
full file name is '/home/HIDDEN/public_html/uploads/remover.php'
Trying to delete
age of /home/HIDDEN/public_html/uploads/remover.php is 0 hours

Thanks in advance
SOLUTION
Member_2_4694817

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Chiehkai

ASKER
Ah, that worked! :)

If I want it not to delete the ".htaccess" file, can I change :

if ($diffhours>3){

into

if (($diffhours>3) && ($file != ".htaccess")){

Thanks again!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.