Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Auto delete all files after x-time
Old 12-17-2009, 07:01 AM Auto delete all files after x-time
Average Talker

Posts: 24
Trades: 0
Hi, all, i comeback again with hug questions... maybe someone here can help me...

how to auto delete all files under a sub directory after x-time (let say after 24 hours) but that i want without using cronjob command from server or pl
just using php code or just visit the page without clicking something and the command auto run, where i can found this script? regards..

Last edited by jones; 12-17-2009 at 07:15 AM..
jones is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-17-2009, 07:28 AM Re: Auto delete all files after x-time
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Do you want to delete all files in the directory every 24 hours, or do you want to delete all files older than 24 hours?
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 12-17-2009, 02:18 PM Re: Auto delete all files after x-time
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
without using cron job then you'll have use mysql database plus a php include in a file that is often executed such as

PHP Code:
<?php

//SQL Connection and you'll need some way to track time
$past_time mysql_query("SELECT * FROM empty_time ORDER BY id DESC LIMIT 1");
$current_time $empty_new_time strtotime("now");

//Empty folder every x hours
$empty_hour 1;

//Empty dir
$empty_dir '/images/';

//Convertint empty time to second
$empty_time $empty_hour 3600;

//Emptying the dir
if ($current_time = ($past_time $empty_time)) {
    if (
is_dir($empty_dir) && $empty_dir!='.' && $empty_dir!='..') {
        if (
$dh opendir($empty_dir)) {
            while (
$file readdir($dh)) {
                if (
$file!='.' || $file!='..') {
                    
unlink($file);    
                }
            }
        }
        
        
//Close Dir
        
closedir($dh);
        
        
//Record New Time
        
mysql_query("INSERT INTO empty_time(new_time) VALUES('$current_time')") or die("Cannot Record New Time");
    }
}


?>
Not tested btw
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 12-18-2009, 04:15 PM Re: Auto delete all files after x-time
Average Talker

Posts: 24
Trades: 0
Quote:
Originally Posted by lizciz View Post
Do you want to delete all files in the directory every 24 hours, or do you want to delete all files older than 24 hours?
i said after x-time (let say after 24 hours) that mean older than 24 hours, but perhaps if you want to make sure all files deleted older than 24 hours, that need to run your code regularly every 24 hours. but thank you for your attentions

Last edited by jones; 12-18-2009 at 04:16 PM..
jones is offline
Reply With Quote
View Public Profile
 
Old 12-18-2009, 04:18 PM Re: Auto delete all files after x-time
Average Talker

Posts: 24
Trades: 0
hi orionoreo
thank you for your code above.. but do you have another code, imaging wihout using database?
jones is offline
Reply With Quote
View Public Profile
 
Old 12-18-2009, 06:41 PM Re: Auto delete all files after x-time
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
would you prefer to write to a txt file instead?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 12-21-2009, 02:05 AM Re: Auto delete all files after x-time
Average Talker

Posts: 24
Trades: 0
hi orionoreo

Quote:
Originally Posted by orionoreo View Post
would you prefer to write to a txt file instead?
i need to delete log/temp file that after 24-hours unused and must auto delete, and now i prefer use flat file.

but i ever hear about using filemtime or filectime do you know about that code?
jones is offline
Reply With Quote
View Public Profile
 
Old 12-21-2009, 12:54 PM Re: Auto delete all files after x-time
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
sorry jones i'm not familiar with that function... so not sure how i would make this from a flat file
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 12-22-2009, 11:44 AM Re: Auto delete all files after x-time
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
$cmd = "nohup find '{$directory}' -type f -cmin +1440 -delete > nohup.out &";
exec($cmd);
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 12-22-2009, 10:18 PM Re: Auto delete all files after x-time
Average Talker

Posts: 24
Trades: 0
@ orionoreo

that's OK, never mind & thankyou for your input

@ mtishetsky

i never seen before the code like this, very simple but i hope will work well
PHP Code:
$cmd "nohup find '{$directory}' -type f -cmin +1440 -delete > nohup.out &";
exec($cmd); 
jones is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Auto delete all files after x-time
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.62279 seconds with 12 queries