Chiehkai
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.
I think it should a simple one, but I couldn't find it anywhere.
Thanks.
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.
<?
$dir = '/home/****/public_html/cr onstuff/';
$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?
$dir = '/home/****/public_html/cr
$dp = opendir($dir) or die ('Could not open '.$dir);
while ($file = readdir($dp)) {
if ((eregi('testfile',$file))
unlink($dir.$file);
}
}
closedir($handle);
?>
Is this something that helps you?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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");
}
// Nothing here
}
else {
// Delete all other files
unlink("yourfile");
}
ASKER
May I know what "eregi('testfile',$file)" does? Do I have to change 'testfile' to something else?
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.
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(),fil ectime($fi le));
with $diff=get_time_difference( time(),fil ectime($di r.$file));
with $diff=get_time_difference(
ASKER
Thanks, but now I get this error :
Parse error: syntax error, unexpected T_IF in PATH_TO_SCRIPT on line 39
Parse error: syntax error, unexpected T_IF in PATH_TO_SCRIPT on line 39
Hmm.. can you show me your line 37 to 39?
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 />");
}
lol.. replace this $diff=get_time_difference( time(),fil ectime($di r.$file)) //getting the file created time
with $diff=get_time_difference( time(),fil ectime($di r.$file)); //getting the file created time
Dude, you forget the semi colon
with $diff=get_time_difference(
Dude, you forget the semi colon
ASKER
Ah, my wrong. Not sure why it was removed.
There's no more errors now, but none of the files are deleted :(
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 ...
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 ...
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.
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?
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);
}
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/me nu_footer. jpg'
Trying to delete
age of /home/HIDDEN/public_html/u ploads/men u_footer.j pg is 0 hours
readdir gives 'Accelerometer Plugin.zip'
full file name is '/home/HIDDEN/public_html/ uploads/Ac celeromete r Plugin.zip'
Trying to delete
age of /home/HIDDEN/public_html/u ploads/Acc elerometer Plugin.zip is 0 hours
readdir gives 'girl.jpg'
full file name is '/home/HIDDEN/public_html/ uploads/gi rl.jpg'
Trying to delete
age of /home/HIDDEN/public_html/u ploads/gir l.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/u ploads/vb_ reference_ sheet.pdf is 0 hours
readdir gives '.htaccess'
full file name is '/home/HIDDEN/public_html/ uploads/.h taccess'
Trying to delete
age of /home/HIDDEN/public_html/u ploads/.ht access is 0 hours
readdir gives 'vb_reference_sheet_1.pdf'
full file name is '/home/HIDDEN/public_html/ uploads/vb _reference _sheet_1.p df'
Trying to delete
age of /home/HIDDEN/public_html/u ploads/vb_ reference_ sheet_1.pd f is 0 hours
readdir gives 'remover.php'
full file name is '/home/HIDDEN/public_html/ uploads/re mover.php'
Trying to delete
age of /home/HIDDEN/public_html/u ploads/rem over.php is 0 hours
Thanks in advance
Below is the output of your extreme-debugging version :
destroying directory '/home/HIDDEN/public_html/
readdir gives '.'
readdir gives '..'
readdir gives 'menu_footer.jpg'
full file name is '/home/HIDDEN/public_html/
Trying to delete
age of /home/HIDDEN/public_html/u
readdir gives 'Accelerometer Plugin.zip'
full file name is '/home/HIDDEN/public_html/
Trying to delete
age of /home/HIDDEN/public_html/u
readdir gives 'girl.jpg'
full file name is '/home/HIDDEN/public_html/
Trying to delete
age of /home/HIDDEN/public_html/u
readdir gives 'vb_reference_sheet.pdf'
full file name is '/home/HIDDEN/public_html/
Trying to delete
age of /home/HIDDEN/public_html/u
readdir gives '.htaccess'
full file name is '/home/HIDDEN/public_html/
Trying to delete
age of /home/HIDDEN/public_html/u
readdir gives 'vb_reference_sheet_1.pdf'
full file name is '/home/HIDDEN/public_html/
Trying to delete
age of /home/HIDDEN/public_html/u
readdir gives 'remover.php'
full file name is '/home/HIDDEN/public_html/
Trying to delete
age of /home/HIDDEN/public_html/u
Thanks in advance
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
If I want it not to delete the ".htaccess" file, can I change :
if ($diffhours>3){
into
if (($diffhours>3) && ($file != ".htaccess")){
Thanks again!
http://www.tizag.com/phpT/filedelete.php
In Windows environments you may try
<?php
chown($TempDirectory."/".$
unlink($TempDirectory."/".
?>
to change permisions to grant deletion of the file.
Hope that helps,
Pancho