Link to home
Start Free TrialLog in
Avatar of Chiehkai
ChiehkaiFlag 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.
Avatar of Panchux
Panchux
Flag of Argentina image

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
Avatar of 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.
Avatar of flwam93
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?
ASKER CERTIFIED SOLUTION
Avatar of mosidiot
mosidiot
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your script mosidiot, can we add a file (say like the .htaccess) that will never be deleted?
if($filename == ".htaaccess") {
// Nothing here
}
else {
// Delete all other files
unlink("yourfile");
}
May I know what "eregi('testfile',$file)" does? Do I have to change 'testfile' to something else?
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.
replace this line $diff=get_time_difference(time(),filectime($file));

with $diff=get_time_difference(time(),filectime($dir.$file));
Thanks, but now I get this error :

Parse error: syntax error, unexpected T_IF in PATH_TO_SCRIPT on line 39
Hmm.. can you show me your line 37 to 39?
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

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

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

There's no more errors now, but none of the files are deleted :(
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 ...
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.
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

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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!